为什么这个 Rails 控制器测试失败?
我试图理解为什么这个测试失败了。 (我对测试有点陌生。)我使用内置的 Rails 测试框架,并添加了 Shoulda gem。
测试:
require 'shoulda'
context "on GET to :new" do
setup do
get(:new)
end
should_render_template :new
should_not_set_the_flash
end
失败:
1) Failure:
test: on GET to :new should render template :new. (SessionsControllerTest)
[/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/controller /macros.rb:220:in `__bind_1233882600_699194'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `call'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `test: on GET to :new should render template :new. '
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run']:
expecting <"new"> but rendering with <"">
2 tests, 2 assertions, 1 failures, 0 errors
但是如果我在控制台上使用 app.get '/sessions/new'
运行它,它可以正常工作,没有错误。
并且“new
”模板在浏览器中按预期呈现。
我正在使用哈姆尔。 也许这引起了问题。 我的模板名为“new.html.haml
”。
I'm trying to understand why this test is failing. (I'm kind of new to testing.) I'm using the built-in Rails testing framework with the addition of the Shoulda gem.
The test:
require 'shoulda'
context "on GET to :new" do
setup do
get(:new)
end
should_render_template :new
should_not_set_the_flash
end
Fails:
1) Failure:
test: on GET to :new should render template :new. (SessionsControllerTest)
[/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/controller /macros.rb:220:in `__bind_1233882600_699194'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `call'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `test: on GET to :new should render template :new. '
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run']:
expecting <"new"> but rendering with <"">
2 tests, 2 assertions, 1 failures, 0 errors
But if I run it on the console with app.get '/sessions/new'
it works fine with no error.
And the "new
" template renders as expected in the browser.
I'm using Haml. Maybe that's causing a problem. My template is called "new.html.haml
".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
失败表明没有渲染任何模板。 尝试确保您没有被重定向
并断言 @response.body 的内容只是为了查看发回的内容
您还可以断言特定模板,这样您也可以尝试一下:
但是,我不'不要怀疑 HAML 是导致您问题的原因。
The failure says that no template has been rendered. Try to make sure you are not being redirected with
and assert the contents of the @response.body just to see what's been sent back
You can also assert a specific template, so you can give it a shot, too:
However, I don't suspect HAML being the cause of your problem.
我在 Shoulda、Clearance 和 Rails 2.3.2 上也遇到了类似的问题。 我想我通过修改应该和清除如何使用“assert_template”解决了这个问题。 显然 Rails 中有一个与此相关的未解决错误。
请参阅此线程以获取更多信息和我的更改差异(Clearance 和 Shoulda 中的一行更改)。
http://groups.google.com/group/shoulda/browse_thread/thread/ 8c0a66c80ff4fd76
I was having a similar problem with Shoulda, Clearance, and Rails 2.3.2. I think I resolved the issue by modifying how shoulda and clearance use 'assert_template'. Apparently there is an open bug in Rails related to this.
Please see this thread for more info and my diff of the changes (one line change in both Clearance and Shoulda).
http://groups.google.com/group/shoulda/browse_thread/thread/8c0a66c80ff4fd76