簡體   English   中英

為什么該Rails控制器測試失敗?

[英]Why is this Rails controller test failing?

我試圖了解為什么此測試失敗。 (我對測試有點陌生。)我正在使用內置的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 ”。

失敗表明尚未渲染任何模板。 嘗試確保您沒有被重定向到

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是造成您問題的原因。

我在Shoulda,Clearance和Rails 2.3.2中遇到了類似的問題。 我認為我通過修改Shoulda和Clearance如何使用'assert_template'解決了該問題。 顯然,Rails中有一個與此相關的開放錯誤。

請查看該線程以獲取更多信息以及我所做的更改差異(“間隙”和“ Shoulda”中的一行更改)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM