Unable to set session hash in Rails 5 controller test

后端 未结 4 1489
醉话见心
醉话见心 2020-12-11 03:23

According to the Rails Edge Guide all ActionDispatch::IntegrationTest HTTP requests take optional named keyword arguments:

get post_url, params: { id: 12 }, se

4条回答
  •  时光说笑
    2020-12-11 03:51

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

提交回复
热议问题