【问题标题】:Why is this Rails controller test failing?为什么这个 Rails 控制器测试失败了?
【发布时间】:2009-02-06 01:25:41
【问题描述】:

我试图了解为什么这个测试失败了。 (我对测试有点陌生。)我正在使用内置的 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”。

【问题讨论】:

    标签: ruby-on-rails testing tdd bdd shoulda


    【解决方案1】:

    失败表示没有渲染模板。尝试确保您没有被重定向到

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

    【讨论】:

    • 谢谢。您建议的对 assert_match 的调用解决了它。我忘记了之前的过滤器搞砸了。
    【解决方案2】:

    我在使用 Shoulda、Clearance 和 Rails 2.3.2 时遇到了类似的问题。我想我通过修改 shoulda 和 clear 使用“assert_template”的方式解决了这个问题。显然,Rails 中有一个与此相关的开放错误。

    请参阅此线程以获取更多信息和我的更改差异(清除和应该更改一行)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多