我试图理解为什么这个测试失败。 (我是一种新的测试。)我使用的是添加了早该宝石的内置Rails的测试框架。

在测试:

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”模板呈现为预期在浏览器中。

我使用Haml的。也许这就是导致问题。我的模板被称为 “new.html.haml”。

有帮助吗?

解决方案

在失败者认为,没有模板已被渲染。尽量确保你不被重定向与

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是你的问题的原因。

其他提示

我在和早该,清除,和Rails 2.3.2类似的问题。我想我通过修改如何早该和清除使用“assert_template”解决了这个问题。显然有在导轨的开口的错误与此有关。

请参阅此线程获取更多信息和我的变化差异(在通关和早该一行变化)。

http://groups.google.com/group/shoulda/browse_thread /线程/ 8c0a66c80ff4fd76

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top