Railsチュートリアル 9章リスト9.25のテストでNomethodErrorが出てしまい原因が分からず困っております。
<エラー内容>
ERROR["test_login_with_remembering", UsersLoginTest, 0.3667849329999626] test_login_with_remembering#UsersLoginTest (0.37s) NoMethodError: NoMethodError: undefined method `log_in_as' for #<UsersLoginTest:0x0000000008692be0> Did you mean? log_files test/integration/users_login_test.rb:41:in `block in <class:UsersLoginTest>' ERROR["test_login_without_remembering", UsersLoginTest, 0.421165880999979] test_login_without_remembering#UsersLoginTest (0.42s) NoMethodError: NoMethodError: undefined method `log_in_as' for #<UsersLoginTest:0x0000000008c42270> Did you mean? log_files test/integration/users_login_test.rb:47:in `block in <class:UsersLoginTest>' 26/26: [=====================================] 100% Time: 00:00:00, Time: 00:00:00 Finished in 0.52865s 26 tests, 64 assertions, 0 failures, 2 errors, 0 skips
<test/integration/users_login_test.rbのソースコード>
require 'test_helper' class UsersLoginTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) end test "login with invalid 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_not_empty cookies['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/helpers/test_helper.rbのソースコード>
ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require "minitest/reporters" Minitest::Reporters.use! class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests # in alphabetical order. fixtures :all include ApplicationHelper # Add more helper methods to be used by all tests here... def is_logged_in? !session[:user_id].nil? end def log_in_as(user) session[:user_id] = user.id end end class ActionDispatch::IntegrationTest def log_in_as(user, password: 'password', remember_me: '1') post login_path, params: { session: { email: user.email, password: password, remember_me: remember_me } } end end
<やってみたこと>
test_helper.rbのclass ActionDispatch::IntegrationTestの中身を
users_login_test.rbのclass UsersLoginTest < ActionDispatch::IntegrationTestに移動したらGreenになりました
なぜエラーが出てしまったのかがわかりません。また、どのようにすれば移動せずにGreenに直せるか知りたいです。どなたか教えていただけると幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/21 01:07
2019/09/21 01:24
2019/09/21 06:46