当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《为什么这个Rails控制器测试失败?》经验,为你挑选了1个好方法。

我试图理解为什么这个测试失败了.(我对测试很陌生.)我正在使用内置的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"模板在浏览器中按预期呈现.

我正在使用Haml.也许这会导致问题.我的模板名为" new.html.haml".



1> Milan Novota..:

失败表示没有渲染模板.尽量确保您没有被重定向

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是导致问题的原因.

推荐阅读
谢谢巷议
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有