Testing a Sinatra application that uses session

后端 未结 4 1158
粉色の甜心
粉色の甜心 2021-01-03 05:23

How to test Sinatra application which is using session?

get "/", {}, {\'rack.session\' =>  { \'foo\' => \'blah\' } }

This cod

4条回答
  •  盖世英雄少女心
    2021-01-03 06:11

    It looks like the problem is actually to have enable :sessions activated.

    You have to deactivate this setting in order to be available to overwrite the session.

    The solution could be:

    # my_test.rb (first line, or at least before you require your 'my_app.rb')
    ENV['RACK_ENV'] = 'test'
    
    # my_app.rb (your sinatra application)
    enable :sessions  unless test?
    
    # my_test.rb (in your test block)
    get '/', {}, 'rack.session' => { :key => 'value' }
    

    In the other hand to be able to check any session change that the action is expected to do we can send not a hash to the rack.session but a pointer to a hash so we can check after the action call if the hash has changed:

    # my_test.rb (in your test block)
    session = {}
    get '/', {}, 'rack.session' => session
    assert_equal 'value', session[:key]
    

提交回复
热议问题