[英]NoMethodError running Integration Test, RailsTutorial Ch9
I'm having an issue where my integration tests do not seem to find the log_in_as method from my test_helper.rb 我遇到一个问题,我的集成测试似乎无法从我的test_helper.rb中找到log_in_as方法
I have been following Michael Hart's Rails tutorial, so I was hoping not to massively refactor my code to try and get this to work. 我一直在关注Michael Hart的Rails教程,所以我希望不要大规模重构我的代码以尝试使其正常工作。 I would like to continue on through the book without having to exclude the tests, since it is pretty test heavy afterall. 我想继续阅读本书,而不必排除测试,因为毕竟这是相当繁重的测试。
Error: 错误:
UsersLoginTest#test_login_with_remembering:
NoMethodError: undefined method `log_in_as' for #<UsersLoginTest:0x00000005b18460>
test/integration/users_login_test.rb:43:in `block in <class:UsersLoginTest>'
User_login_test.rb: User_login_test.rb:
require 'test_helper.rb'
class UsersLoginTest < ActionDispatch::IntegrationTest
.
.
.
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 to set the cookie.
log_in_as(@user, remember_me: '1')
# Log in again and verify that the cookie is deleted.
log_in_as(@user, remember_me: '0')
assert_empty cookies['remember_token']
end
end
test_helper.rb: test_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
class ActiveSupport::TestCase
fixtures :all
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Log in as a particular user.
def log_in_as(user)
session[:user_id] = user.id
end
end
class ActionDispatch::IntegrationTest
# Log in as a particular user.
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
I had this same issue. 我有同样的问题。 There are two problems I had to fix: 我必须解决两个问题:
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.