当需要采取行动来拯救异常时,帮助让 Rails 控制器测试通过
我想测试一个非常简单的控制器操作。
it "should render 404 page if template does not exist" do
get 'show', :page => "does_not_exist"
response.should render_template("/public/404")
end
基本上我想要一个动态呈现静态页面的操作。这样我就不必为 N 个页面执行 N 个操作。
该测试的实现如下:
def show
begin
render(params[:page])
rescue ActionView::MissingTemplate
render("/public/404")
end
end
这段代码确实有效,但测试还是失败了。这对我来说没有什么意义,因为测试报告异常被抛出......即使控制器应该捕获它:
ActionView::MissingTemplate: Missing template webpages/does_not_exist with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/egervari/Projects/training/app/views", "/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.3.4/app/views"
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_view/paths.rb:15:in `find'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_view/lookup_context.rb:81:in `find'
控制器方法退出后是否抛出异常或类似的事情?我怎样才能通过测试?
谢谢!
编辑:事实证明我没有做错任何事。问题是因为我有 spork 正在运行,我需要重新启动它。我开始认为 Spork 带来的麻烦超过了它的价值。我是 ruby 和 Rails 的新手,所以我很难判断什么是合法错误以及何时是由于 spork 造成的。也许我不应该在下个月之前使用 spork。
I have a really simple controller action I'd like to test.
it "should render 404 page if template does not exist" do
get 'show', :page => "does_not_exist"
response.should render_template("/public/404")
end
Basically I want to have an action that dynamically renders a static page. That way I don't have to have N actions for N pages.
The implementation for this test is as follows:
def show
begin
render(params[:page])
rescue ActionView::MissingTemplate
render("/public/404")
end
end
This code actually works, but the test fails anyway. It doesn't make the most sense to me, because the test reports that the exception is getting thrown... even though the controller is supposed to catch it:
ActionView::MissingTemplate: Missing template webpages/does_not_exist with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/egervari/Projects/training/app/views", "/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.3.4/app/views"
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_view/paths.rb:15:in `find'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_view/lookup_context.rb:81:in `find'
Is the exception getting thrown after the controller method exits or something like that? How can I get my test to pass?
Thanks!
EDIT: It turns out I did nothing wrong. The problem was because I had spork running and I needed to restart it. I am starting to think that Spork is more trouble than it's worth. I am new to ruby and rails, so it's hard for me to tell what is a legitimate error and when it's due to spork. Maybe I shouldn't use spork until next month or something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该转储 result.body:
然后根据您的假设进行检查,您期望看到 404 页面,但您可能会收到它在开发模式下为您呈现的 Rails 错误页面。
伊恩。
You should dump the result.body:
Then check this against your assumptions, you are expecting the 404 page, but you may be getting the Rails error page which it renders for you in development mode.
ian.
请参阅相关问题:Basic Rails 404 错误页面
See relevant question: Basic Rails 404 Error Page