RSpec set session object

前端 未结 4 1369
鱼传尺愫
鱼传尺愫 2020-12-15 03:11

I\'m trying to set a session object from my controller spec.

it \'use invalid coupon\' do
  session[:coupon] = \'abcd\'      
  Service.any_instance.stub(:va         


        
4条回答
  •  悲&欢浪女
    2020-12-15 03:56

    In Rails 5+, if you are using ActionController::TestCase session is passed as a keyword arg.

    Setting params and session would look like:

    get(:show, params: {'id' => "12"}, session: {'user_id' => 5})
    

    Setting only the session would look like,

    get(:show, session: {'user_id' => 5})
    

    If you are using ActionDispatch::IntegrationTest, which is the new default for for controller tests, you are not able to set the session variables and should set them by walking your test through the login flow.

提交回复
热议问题