이 레일 컨트롤러 테스트가 실패한 이유는 무엇입니까?
-
21-08-2019 - |
문제
이 테스트가 왜 실패했는지 이해하려고합니다. (저는 테스트에 익숙하지 않습니다.) Thinga 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의 내용을 주장합니다.
# 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이 당신의 문제의 원인이라고 생각하지 않습니다.
다른 팁
나는 Dooksa, Clearance 및 Rails 2.3.2와 비슷한 문제를 겪고있었습니다. 나는 whita and clearance가 'assert_template'을 사용하는 방법을 수정하여 문제를 해결했다고 생각합니다. 분명히 이와 관련된 레일에는 열린 버그가 있습니다.
자세한 내용과 변경 사항에 대한 자세한 내용은이 스레드를 참조하십시오 (클리어런스와 Thoulda의 한 줄 변경).
http://groups.google.com/group/shoulda/browse_thread/thread/8c0a66c80ff4fd76
제휴하지 않습니다 StackOverflow