ruby on rails チュートリアル(5.1版)の10章で詰まっています。
リスト 10.62で、以下のコードのテストをしようとしたところ、assertでエラーが発生しました。
デバッグ方法も限られており、原因が全く分からず途方に暮れています。。
すみませんが、よろしくお願いします。
[test/integration/users_index_test.rb]
test "index as admin including pagination and delete links" do log_in_as(@admin) get users_path assert_template 'users/index' assert_select 'div.pagination' first_page_of_users = User.paginate(page: 1) first_page_of_users.each do |user| assert_select 'a[href=?]', user_path(user), text: user.name unless user == @admin assert_select 'a[href=?]', user_path(user), text: 'delete' end end assert_difference 'User.count', -1 do delete user_path(@non_admin) end end
[error]
ERROR["test_index_including_pagination", Minitest::Result, 0.9957311759935692] test_index_including_pagination#Minitest::Result (1.00s) Minitest::UnexpectedError: NoMethodError: undefined method `email' for nil:NilClass test/test_helper.rb:26:in `log_in_as' test/integration/users_index_test.rb:16:in `block in <class:UsersIndexTest>' FAIL["test_index_as_admin_including_pagination_and_delete_links", Minitest::Result, 1.0364086409681477] test_index_as_admin_including_pagination_and_delete_links#Minitest::Result (1.04s) Expected at least 1 element matching "a[href="/users/1028376774"]", found 0.. Expected 0 to be >= 1. test/integration/users_index_test.rb:41:in `block (2 levels) in <class:UsersIndexTest>' test/integration/users_index_test.rb:40:in `block in <class:UsersIndexTest>'
エラーの具体的な個所が分からなかったため、putsあたりで評価中のユーザ名を指定したところ、User-6で必ずエラーになっていることが分かりました(必ずここでエラーが発生)。
[デバッグを入れたエラー結果]
Sterling Archer duchess@example.gov= ] 34% Time: 00:00:00, ETA: 00:00:01 Lana Kane hands@example.gov Malory Archer boss@example.gov User 0 user-0@example.com User 1 user-1@example.com User 2 user-2@example.com User 3 user-3@example.com User 4 user-4@example.com User 5 user-5@example.com User 6 user-6@example.com FAIL["test_index_as_admin_including_pagination_and_delete_links", Minitest::Result, 0.6730378799838945] test_index_as_admin_including_pagination_and_delete_links#Minitest::Result (0.67s) Expected at least 1 element matching "a[href="/users/1028376774"]", found 0.. Expected 0 to be >= 1. test/integration/users_index_test.rb:42:in `block (2 levels) in <class:UsersIndexTest>' test/integration/users_index_test.rb:40:in `block in <class:UsersIndexTest>'
fixturesのusers.ymlもチュートリアル通りで特に問題なさそうです。。
michael: name: Michael Example email: michael@example.com2 password_digest: <%= User.digest('password') %> admin: true archer: name: Sterling Archer email: duchess@example.gov password_digest: <%= User.digest('password') %> lana: name: Lana Kane email: hands@example.gov password_digest: <%= User.digest('password') %> malory: name: Malory Archer email: boss@example.gov password_digest: <%= User.digest('password') %> <% 30.times do |n| %> user_<%= n %>: name: <%= "User #{n}" %> email: <%= "user-#{n}@example.com" %> password_digest: <%= User.digest('password') %> <% end %>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/24 00:01
2020/06/24 00:04
2020/06/24 09:35
2020/06/24 23:01
2020/06/27 01:21