Why does assert_response ignore a custom message in Rails?

In my functional test, I check the status of the response as follows:

assert_response :error, "My error message" 

However, Rails ignores my posts and reports:

Expected answer: a <: error>, but was <201>

Any ideas why?

I am using Rails 3.2.6.

+4
source share
2 answers

I was wondering the same thing. Turns out this was fixed on Github: https://github.com/rails/rails/commit/d28a15ede59f6434f1b7a8d01be060fa73b4746c

For me, updating the rails / actionpack gem to the latest version did not include this fix, but I was able to update response.rb manually. It was located at:

 /Users/alex/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/testing/assertions/response.rb 
+1
source

In your test, you mentioned the type of response as: an error that corresponds to status code 500-599. But as your test returns a status code of 201, it cannot match this and show the message according to the definition of assert_response, which is

 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 

it switches to another due to a mismatch of the type and status of the code and gives this message.

Here are the response type and match codes:

 :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 
0
source

All Articles