質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

15438閲覧

uninitialized constant がRSpecすると出てしまう

hrc

総合スコア55

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2017/02/10 00:16

Deviseで作成したフォームにRSpecでテストしようとしています。
以下のようなエラーが出て困っております。どのようにしたら直りますでしょうか?
ご教示頂ければ幸いです。

bash

1$ bin/rspec 2Running via Spring preloader in process 18113 3/Users/noriakitakamizawa/xxx/spec/controller/test_login_spec.rb:3:in `<top (required)>': uninitialized constant TestLoginController (NameError) 4 from /Users/noriakitakamizawa/xxx/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files' 5 from /Users/noriakitakamizawa/xxx/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each' 6 from /Users/noriakitakamizawa/xxx/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files' 7

テストコードはこちらです。
spec/controller/test_login_spec.rb

ruby

1require 'rails_helper' 2 3describe TestLoginController do 4 login_admin 5 6 it "should have a current_user" do 7 # note the fact that you should remove the "validate_session" parameter if this was a scaffold-generated controller 8 expect(subject.current_user).to_not eq(nil) 9 end 10 11 it "should get index" do 12 # Note, rails 3.x scaffolding may add lines like get :index, {}, valid_session 13 # the valid_session overrides the devise login. Remove the valid_session from your specs 14 get 'index' 15 response.should be_success 16 end 17end

spec/rails_helper.rb

ruby

1ENV['RAILS_ENV'] ||= 'test' 2require File.expand_path('../../config/environment', __FILE__) 3abort("The Rails environment is running in production mode!") if Rails.env.production? 4require 'spec_helper' 5require 'rspec/rails' 6require 'devise' 7require 'rspec/autorun' 8 9Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 10ActiveRecord::Migration.maintain_test_schema! 11 12RSpec.configure do |config| 13 config.include Devise::Test::ControllerHelpers, :type => :controller 14 15 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 16 config.fixture_path = "#{::Rails.root}/spec/fixtures" 17 18 # If you're not using ActiveRecord, or you'd prefer not to run each of your 19 # examples within a transaction, remove the following line or assign false 20 # instead of true. 21 config.use_transactional_fixtures = true

spec/spec_helper.rb

ruby

1ENV["RAILS_ENV"] ||= 'test' 2require File.expand_path("../../config/environment", __FILE__) 3 4require 'rspec/rails' 5require 'rspec/autorun' 6require 'capybara/rails' 7require 'devise' 8Rails.logger = Logger.new(STDOUT) 9 10RSpec.configure do |config| 11 config.include Rails.application.routes.url_helpers 12 config.include Capybara::DSL 13 14 # for Devise 15 #config.include Devise::TestHelpers, :type => :controller 16 #config.include Capybara::DSL 17 config.include Devise::Test::ControllerHelpers, :type => :controller

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

3行目を
describe "TestLoginController" do
にしてみてください

投稿2017/02/10 10:06

glvty83

総合スコア135

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hrc

2017/02/11 00:00

実施してみたところlogin_adminでエラーで引っかかりましたが、コメントアウトしたら動き出しました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問