使用会话 sinatra 进行 Rspec 测试
我有使用会话的 sinatra 应用程序,如何测试使用会话的页面?
我使用 Rspec + sinatra
Tks
I have sinatra appliction using session, how do I test a page that uses session?
I using Rspec + sinatra
Tks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此页面,展示了测试会话的主要问题与
rspec
和sinatra
。主要问题是变量
session
无法从测试代码访问。为了解决这个问题,它建议(并且我确认)在spec/spec_helper.rb
添加 fowling 行:它将允许从测试代码的最后一个请求访问模拟会话对象。
这是我的控制器代码,它读取
code
参数并将其值存储在session
中:然后是测试代码:
This page, show's the main problem of testing session with
rspec
andsinatra
.The main problem is that the variable
session
is not accessible from the test code. To solve it, it suggests (and I confirm) to add the fowling lines atspec/spec_helper.rb
:It will make accessible a mocked session object from the last request at the test code.
Here's my controller code that read's a
code
param and stores its value atsession
:Then here's test code:
与所有其他页面相同。
你需要澄清你的问题。测试会话有什么问题。如果问题是用户需要登录,那么您可以在规范文件中使用类似的内容:
它将在每次测试之前让您登录。
Same like all other pages.
You need to clarify your question. What is problem with testing session. If problems is that user need to be logged in, then you can use something like this in spec file:
which will log you in before each test.
我只能通过禁用测试环境的会话来使其工作。这篇博文有一个很好的例子: http://benprew.posterous.com/testing-sessions -with-sinatra
I was only able to get it to work by disabling sessions for the test environment. This blog post has a good example: http://benprew.posterous.com/testing-sessions-with-sinatra