これはなぜ失敗コントローラーのテストをRailsはありますか?
-
21-08-2019 - |
質問
私は、このテストが失敗している理由を理解しようとしています。 (私はテストに新しいの一種だ。)私はShoulda宝石を加えて内蔵の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が問題の原因である疑いはありません。
他のヒント
私はShoulda、クリアランス、およびRails 2.3.2と同様の問題を抱えていました。私はどのようにshouldaとクリアランス使用「assert_template」を変更することで問題を解決だと思います。どうやらこれに関連したレールで開いバグがあります。
この詳細は、スレッドや変更の私の差分(クリアランスとShouldaの両方で1行の変更)を参照してください。
http://groups.google.com/group/shoulda/browse_thread /スレッド/ 8c0a66c80ff4fd76する
所属していません StackOverflow