如何在不测试视图的情况下测试 sinatra (padrino) 控制器?

发布于 2024-10-27 01:00:21 字数 331 浏览 8 评论 0原文

有什么好的方法可以用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2024-11-03 01:00:21

对于这种情况,您似乎想测试:
a) DateTime.now 为您提供一个有效值 - 直接使用经典 rspec 进行测试。
b) var some_var 被传递给渲染,但这很简单。

听起来您可能实际上想单独测试视图。像这样的东西(未经测试):

it 'shows the time now' do
    right_now = DateTime.now
    engine = Haml::Engine.new(IO.read('some_view.haml'))
    rendered = engine.render(Object.new, :@some_var => right_now)
    rendered.should have_css('ul#some_var li', :text => right_now)
end 

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):

it 'shows the time now' do
    right_now = DateTime.now
    engine = Haml::Engine.new(IO.read('some_view.haml'))
    rendered = engine.render(Object.new, :@some_var => right_now)
    rendered.should have_css('ul#some_var li', :text => right_now)
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文