Question: *Agile Web Development With Rails4* gives an example test code like this:

*carts\_controller\_test.rb*:

```
test "should destroy cart" do
  assert_difference('Cart.count', -1) do
    session[:cart_id] = @cart.id
    ...

```

However, when I try this in rails 5

```
>>rake test

```

an error is raised

> 
> NoMethodError: undefined method `session' for
>  nil:NilClass
> 
> 
> 

How to use session in the test controller in rails 5? Or did I overlook some important config?

Answer: 
In Rails 5 it is no longer possible to access session in controller tests: <http://blog.napcs.com/2016/07/03/upgrading-rails-5-controller-tests/>. The suggestion is to access the controller method that would set the appropriate session value for you. This comment shows and example of how you might work around this limitation: <https://github.com/rails/rails/issues/23386#issuecomment-192954569>

```
  # helper method
  def sign_in_as(name)
    post login_url, params: { sig: users(name).perishable_signature )
  end

class SomeControllerTest
  setup { sign_in_as 'david' }

  test 'the truth' do
  ..

```
I have found the answer in the [Unable to set session hash in Rails 5 controller test](https://stackoverflow.com/questions/37796129/unable-to-set-session-hash-in-rails-5-controller-test)

Test controllers of rails5 inherit from ActionDispatch::IntegrationTest by default while rails4 from ActionController::TestCase. Session is not available in the context of ActionDispatch::IntegrationTest.