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/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

在您的测试中,您提到响应类型为 :error,它与状态码 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

由于类型和状态代码不匹配,它进入 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 回答