问题
I'm a Rails beginner working through Michael Hartl's Rails Tutorial and am receiving an error I have no idea how to fix. For reference, this is for implementing the changes in listing 9.24 (https://www.railstutorial.org/book/advanced_login).
I skipped chapter 9 (since it is supposedly optional) but in Chapter 10 it asks to include the changes made in listing 9.24 so I did and my tests are still failing.
This is the error I am receiving when I run rails test
Error:
UsersEditTest#test_unsuccessful_edit:
NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:18:in `log_in_as'
test/integration/users_edit_test.rb:14:in `block in <class:UsersEditTest>'
bin/rails test test/integration/users_edit_test.rb:12
E
Error:
UsersEditTest#test_successful_edit:
NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:18:in `log_in_as'
test/integration/users_edit_test.rb:28:in `block in <class:UsersEditTest>'
The tests (in test/integration/users_edit_test.rb) that are failing are:
test "successful edit" do
log_in_as(@user)
get edit_user_path(@user)
... end
test "unsuccessful edit" do
log_in_as(@user)
get edit_user_path(@user)
... end
and here is the integration/test_helper method that is being called
# Log in as a particular user.
def log_in_as(user)
session[:user_id] = user.id
end
What is especially confusing is that there is another method in the test helper that also uses sessions, and is called in user_login_test which works fine.
Any help would be greatly appreciated!!
回答1:
For the benefit of anyone coming across this question in the future, for integration tests you'll need to define a test_helper method that posts to the session controllers create method, not modify the session specifically e.g.
class ActionDispatch::IntegrationTest
def log_in_as(user, password: 'password')
post login_path, params: { session: { email: user.email, password: password} }
end
end
The reason your other session method works is because it's not assigning anything to the session hash, just checking if a value exists or not. For integration tests, you can't modify the session hash directly with permanence.
回答2:
Session is only available after first request in test cases. Your log_in_as(user)
helper method could initiate the request that actually logs the user in so that session will be filled with user.id
.
Check out the discussion in this thread:
https://github.com/rails/rails/issues/23386#issuecomment-192954569
来源:https://stackoverflow.com/questions/44461101/accessing-session-in-integration-test