在我的功能測試,我確認那樣的響應狀態:爲什麼「assert_response」忽略Rails中的自定義消息?
assert_response :error, "My error message"
但是,Rails的忽略我的自定義信息和報告:
期望的迴應是一個<:錯誤>,但< 201>
任何想法爲什麼?
我使用Rails 3.2.6。
在我的功能測試,我確認那樣的響應狀態:爲什麼「assert_response」忽略Rails中的自定義消息?
assert_response :error, "My error message"
但是,Rails的忽略我的自定義信息和報告:
期望的迴應是一個<:錯誤>,但< 201>
任何想法爲什麼?
我使用Rails 3.2.6。
在你的測試中,你已經提到了響應類型,如:與狀態碼500-599匹配的錯誤。但隨着你的測試返回狀態碼201它不能根據assert_response的定義是
def assert_response(type, message = nil)
clean_backtrace do
if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
assert_block("") { true } # to count the assertion
elsif type.is_a?(Fixnum) && @response.response_code == type
assert_block("") { true } # to count the assertion
else
assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
end
end
end
它進入別的由於類型和狀態代碼不匹配,並給出了相匹配,並顯示該消息信息。
這裏有響應的類型及其配套代碼:
:success: Status code was 200
:redirect: Status code was in the 300-399 range
:missing: Status code was 404
:error: Status code was in the 500-599 range
我想知道同樣的事情。事實證明,它已被固定在Github上:https://github.com/rails/rails/commit/d28a15ede59f6434f1b7a8d01be060fa73b4746c
對我來說,更新rails/actionpack gem到最新版本並沒有包括那個修復,但是我能夠手動更新response.rb。它位於:
/Users/alex/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/testing/assertions/response.rb
謝謝你指出這一點! – MrYoshiji 2014-02-18 14:52:19
@Misha有幫助嗎?或者它有任何問題 – abhas 2012-08-02 11:36:44
請注意,還有更多的狀態可以被聲明。這裏有一個完整的列表:https://github.com/chneukirchen/rack/blob/master/lib/rack/utils.rb#L478 – MBHNYC 2012-11-05 19:30:43