aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/assertions
diff options
context:
space:
mode:
authorKir Shatrov <shatrov@me.com>2016-09-12 17:56:38 -0400
committerKir Shatrov <shatrov@me.com>2016-09-14 11:30:09 -0400
commitbc3b0e729282b6474f217806c14f293584dd8c97 (patch)
tree65c9914c4270ed1c49fb6ea27c9929da7d07a907 /actionpack/test/assertions
parentcf5f55cd30aef0f90300c7c8f333060fe258cd8a (diff)
downloadrails-bc3b0e729282b6474f217806c14f293584dd8c97.tar.gz
rails-bc3b0e729282b6474f217806c14f293584dd8c97.tar.bz2
rails-bc3b0e729282b6474f217806c14f293584dd8c97.zip
Improve assert_response helper
When the check is failed, print the actual response body if it's not too large. This could improve productivity when writing new tests. Before: ``` ThemeEditorIntegrationTest#test_whatever Expected response to be a <200: ok>, but was a <422: Unprocessable Entity>. Expected: 200 Actual: 422 ``` After: ``` ThemeEditorIntegrationTest#test_whatever Expected response to be a <200: ok>, but was a <422: Unprocessable Entity>. Expected: 200 Actual: 422 Response body: {"errors":["Invalid settings object for section '1'"]} ```
Diffstat (limited to 'actionpack/test/assertions')
-rw-r--r--actionpack/test/assertions/response_assertions_test.rb24
1 files changed, 23 insertions, 1 deletions
diff --git a/actionpack/test/assertions/response_assertions_test.rb b/actionpack/test/assertions/response_assertions_test.rb
index 17517f8965..14a04ccdb1 100644
--- a/actionpack/test/assertions/response_assertions_test.rb
+++ b/actionpack/test/assertions/response_assertions_test.rb
@@ -6,10 +6,11 @@ module ActionDispatch
class ResponseAssertionsTest < ActiveSupport::TestCase
include ResponseAssertions
- FakeResponse = Struct.new(:response_code, :location) do
+ FakeResponse = Struct.new(:response_code, :location, :body) do
def initialize(*)
super
self.location ||= "http://test.example.com/posts"
+ self.body ||= ""
end
[:successful, :not_found, :redirection, :server_error].each do |sym|
@@ -112,6 +113,27 @@ module ActionDispatch
" redirect to <http://test.host/posts/redirect/2>"
assert_match expected, error.message
end
+
+ def test_error_message_shows_short_response_body
+ @response = ActionDispatch::Response.new
+ @response.status = 400
+ @response.body = "not too long"
+ error = assert_raises(Minitest::Assertion) { assert_response 200 }
+ expected = "Expected response to be a <200: OK>,"\
+ " but was a <400: Bad Request>" \
+ "\nResponse body: not too long"
+ assert_match expected, error.message
+ end
+
+ def test_error_message_does_not_show_long_response_body
+ @response = ActionDispatch::Response.new
+ @response.status = 400
+ @response.body = "not too long" * 50
+ error = assert_raises(Minitest::Assertion) { assert_response 200 }
+ expected = "Expected response to be a <200: OK>,"\
+ " but was a <400: Bad Request>"
+ assert_match expected, error.message
+ end
end
end
end