我有一个使用session的sinatra应用程序,如何测试使用session的页面?
我使用Rspec + sinatra。
我使用Rspec + sinatra。
这个页面展示了使用rspec
和sinatra
进行测试会话的主要问题。
主要问题在于测试代码无法访问变量session
。为了解决这个问题,建议(我也确认)在spec/spec_helper.rb
中添加以下行:
def session
last_request.env['rack.session']
end
post '/step2callback' do
session['code'] = params['code']
end
context "making an authentication" do
before do
post "/step2callback", code: "code_sample"
end
it "reads the param code and saves it at session" do
expect(session['code']).to eq("code_sample")
end
end
和其他页面一样。
您需要澄清您的问题。测试会话有什么问题?如果问题是用户需要登录,则可以在规范文件中使用以下内容:
before :each do
post "/login", {:username => "myuser", :password => "mypass"}
end
这将在每个测试之前登录您的账户。
我只能通过禁用测试环境的会话才能使其正常工作。这篇博客文章有一个很好的例子:http://benprew.posterous.com/testing-sessions-with-sinatra