Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing session in integration test

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!!

like image 843
Yalon Gordon Avatar asked Jun 09 '17 15:06

Yalon Gordon


People also ask

Which technique is used for integration testing?

Integration testing is performed using the black box method. This method implies that a testing team interacts with an app and its units via the user interface – by clicking on buttons and links, scrolling, swiping, etc. They don't need to know how code works or consider the backend part of the components.

Can you use Jest for integration testing?

In general, Jest can run seven integration tests in parallel. The biggest impact on the duration for integration tests is the starting of the different docker containers.


2 Answers

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.

like image 90
seasalty Avatar answered Sep 28 '22 04:09

seasalty


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

like image 23
William Dias Avatar answered Sep 28 '22 03:09

William Dias