4

私の機能テストでは、次のような応答ステータスを確認します。

assert_response :error, "My error message"

ただし、Rails はカスタム メッセージとレポートを無視します。

<:error> の応答が期待されていましたが、<201> でした

理由はありますか?

Rails 3.2.6 を使用しています。

4

2 に答える 2

1

私は同じことを疑問に思っていました。Github で修正されていることがわかりました: https://github.com/rails/rails/commit/d28a15ede59f6434f1b7a8d01be060fa73b4746c

私の場合、rails/the 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
于 2012-10-31T11:49:29.727 に答える
0

あなたのテストでは、ステータス コード 500-599 に一致する :error として応答タイプを言及しました。しかし、あなたのテストはステータスコード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

タイプとステータスコードの不一致が原因でelseになり、そのメッセージが表示されます。

応答のタイプとそれに対応するコードは次のとおりです。

: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
于 2012-07-12T17:06:27.987 に答える