为什么这个 Rails 控制器测试失败?

发布于 2024-07-13 18:45:13 字数 1273 浏览 9 评论 0原文

我试图理解为什么这个测试失败了。 (我对测试有点陌生。)我使用内置的 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 技术交流群。

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

发布评论

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

评论(2

舂唻埖巳落 2024-07-20 18:45:13

失败表明没有渲染任何模板。 尝试确保您没有被重定向

should_respond_with :success

并断言 @response.body 的内容只是为了查看发回的内容

# This will fail but should give you some clue about what was sent back.
should "sent something back in the body" do
  assert_match 'boohooo', @response.body
end

您还可以断言特定模板,这样您也可以尝试一下:

should_render_template "new.html.haml"

但是,我不'不要怀疑 HAML 是导致您问题的原因。

The failure says that no template has been rendered. Try to make sure you are not being redirected with

should_respond_with :success

and assert the contents of the @response.body just to see what's been sent back

# This will fail but should give you some clue about what was sent back.
should "sent something back in the body" do
  assert_match 'boohooo', @response.body
end

You can also assert a specific template, so you can give it a shot, too:

should_render_template "new.html.haml"

However, I don't suspect HAML being the cause of your problem.

软糯酥胸 2024-07-20 18:45:13

我在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文