如何在不测试视图的情况下测试 sinatra (padrino) 控制器?
有什么好的方法可以用 rspec 测试控制器吗?实际上,我正在使用rack/test测试完整的输出(包括渲染的视图)。
以以下最小控制器为例:
MyApp.controllers :cool_controller do
get :index do
some_var = DateTime.now
render 'some_view', :locals => { :dont_know => nil, :some_var => some_var }
end
end
测试本地哈希中的值是否像我期望的那样的最佳选项是什么?
is there any good way to test a controller with rspec? Actually, I'm testing the complete output (including the rendered view) with rack/test.
Take the following minimal controller for example:
MyApp.controllers :cool_controller do
get :index do
some_var = DateTime.now
render 'some_view', :locals => { :dont_know => nil, :some_var => some_var }
end
end
What would be the best option to test, that the values in the locals hash are like I expect them to be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这种情况,您似乎想测试:
a) DateTime.now 为您提供一个有效值 - 直接使用经典 rspec 进行测试。
b) var some_var 被传递给渲染,但这很简单。
听起来您可能实际上想单独测试视图。像这样的东西(未经测试):
For this case, it seems you want to test:
a) DateTime.now gives you a valid value - test that directly using classical rspec.
b) the var some_var is passed to render, but that is trivial.
It sounds like you might actually want to test the view in isolation. Somthing like this (untested):