西纳特拉 +机架::测试 + Rspec2 - 使用会话?
这是我第一次使用 Sinatra,但我无法让会话在我的测试中发挥作用。我在我的应用程序中启用了 :sessions 。
我尝试过:
get "/controller/something", {}, "rack.session" => {:session => "Aa"}
或者
get "/controller/something", {}, "session" => {:session => "Aa"}
但是我的请求中没有设置会话变量。我浏览了网络并尝试了一些建议,但似乎没有任何效果。我错过了什么吗?
谢谢!
It's the first time I'm working with Sinatra and I just can't get sessions to work in my tests. I have enable :sessions
in my app.
I tried:
get "/controller/something", {}, "rack.session" => {:session => "Aa"}
or
get "/controller/something", {}, "session" => {:session => "Aa"}
But no session variables are being set in my request. I've looked around the web and tried several suggestions but nothing seems to work. Am I missing something?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rack 不再支持通过请求传入会话(Rack >= v1.0)。 阅读这篇文章以获取更多详细信息。
在应用程序中设置会话变量的最佳方法是在应用程序内部调用一个操作来设置会话变量。例如,如果您的应用程序内有一个设置如下会话变量的路由:
让我们假设您实际上想要测试另一个使用会话变量的路由,如下所示:
然后在测试中,您应该首先调用该路由它设置会话,然后转到使用它的另一条路由。这是 rspec 表示法,因为这就是我用于测试的方法:
如果您要在测试中频繁使用该路由,那么您可以在测试文件中编写一个方法来完成此操作(如果您使用 Rspec,则这个方法可以放在你的spec_helper.rb或你的controller_spec.rb文件中):
然后在你需要设置它时在你的测试中调用它:
Rack doesn't support passing in sessions via the request anymore (Rack >= v1.0). Read this post for more detailed information on that.
The best way to set a session variable in your app is to call an action inside of your application that will set the session variable. For instance, if you have a route inside your app that sets a session variable like this:
Let's pretend there's another route that you actually wanted to test which is using the session variable like this:
Then in your tests, you should first call the route which sets the session, then go onto another route which uses it. Here is rspec notation, since that is what I use for testing:
If you were going to be using the route frequently in your tests, then you could write a method to accomplish this in your test file (if you're using Rspec, then this method could go in your spec_helper.rb or in your controller_spec.rb file):
and then call it in your tests when you needed it to be set:
您需要使用最终位于
env
中的密钥:You need to use the keys that will end up in
env
: