004. rails 5 controller test session - cwy007/tips-and-skills GitHub Wiki
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
..
reference link: https://stackoverflow.com/questions/37796129/unable-to-set-session-hash-in-rails-5-controller-test
7.4 The Three Hashes of the Apocalypse
After a request has been made and processed, you will have 3 Hash objects ready for use:
cookies - Any cookies that are set flash - Any objects living in the flash session - Any object living in session variables As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name. For example:
flash["gordon"] flash[:gordon]
session["shmession"] session[:shmession]
cookies["are_good_for_u"] cookies[:are_good_for_u]
ref: http://edgeguides.rubyonrails.org/testing.html#the-three-hashes-of-the-apocalypse