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

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

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

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

0回答

1186閲覧

railstutorial 9章 NoMethodError: undefined method `[]' for nil:NilClass

kdkdkd

総合スコア4

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2020/05/24 11:01

前提・実現したいこと

testを行う際に、users.ymlにテストデータを入れて,setupメソッドで呼び出していたら以下のようなエラーが出てしまい、
困っています

発生している問題・エラーメッセージ

エラーメッセージ /catch/test/integration/users_login_test.rb:9:in `setup' /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/fixtures.rb:916:in `block (3 levels) in setup_fixture_accessors' Error: test: login with incalid information(UsersLoginTest): NoMethodError: undefined method `[]' for nil:NilClass

該当のソースコード

rails5.2
test/integration/users_login_test.rb

ソースコード require 'test_helper' class UsersLoginTest < ActionDispatch::IntegrationTest # test "the truth" do # assert true # end def setup @user = users(:michael) end test "login with incalid information" do get login_path assert_template 'sessions/new' post login_path, params: { session: { email: "", password: "" }} assert_template 'sessions/new' assert_not flash.empty? get root_path assert flash.empty? end test "login with valid information" do get login_path post login_path, params: { session: { email: @user.email, password: 'password' } } assert is_logged_in? assert_redirected_to @user follow_redirect! assert_template 'users/show' assert_select "a[href=?]", login_path, count: 0 assert_select "a[href=?]", logout_path assert_select "a[href=?]", user_path(@user) delete logout_path assert_not is_logged_in? assert_redirected_to root_url delete logout_path follow_redirect! assert_select "a[href=?]", login_path assert_select "a[href=?]", logout_path, count: 0 assert_select "a[href=?]", user_path(@user), count: 0 end test "login with remembering" do log_in_as(@user, remember_me: '1') assert_equal cookies['remember_token'], assigns(:user).remember_token end test "login without remembering" do log_in_as(@user, remember_me: '1') delete logout_path log_in_as(@user, remember_me: '0') assert_empty cookies['remember_token'] end end ------------------------------ test/fixtures/users.yml # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html michael: name: Michael Example email: michael@example.com password_digest: <%= User.digest('password') %>

試したこと

def setup
end
ではなく
setup do
end
にすると動きました
ですが、なぜこうなっているのかが全然わかっていません
教えていただけると幸いです
よろしくお願いいたします

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問