assert-response: sugar around Rack::Test
When it comes to Rack::Test there are two things that I don't like:
- If your rack app raises an error, it is not exposed as an error but hidden in last_response.errors
- Most of the time a status 404 should be considered as an error - the only exeption being that we expect a 404 status
- It's annoying to test for content-types
assert-response is there to address these issues:
- an error that occurs in your rack app will be raised (with original backtrace)
- you may test the error class and message
- a status 404 will raise an error everywhere execept from testing 404 responses
- you get a shortcut for all content-types that come with Rack::Mime and may register your own
- you get a little DSL to be able to do several test against the same response in a DRY way
assert-response has been tested with ruby 1.8.7 and 1.9.2. It should be usable with every testing library and has been tested with test/unit and minitest.
Install it as a gem:
sudo gem install assert-response
or in rvm:
gem install assert-response
require 'minitest' require 'rack/test' require 'assert-reponse' include Rack::Test::Methods def app MyApp end
(for usage without the inclusion of Rack::Test::Methods or with Test::Unit, see AssertResponse::Methods)
And then in your test something like this:
get '/myroute' assert_response_html 'my body'
# raise the error, if an error occures in MyApp raise AssertResponse::Status404 if last_response.status is not 404 assert_equal200, last_response.status assert "test/html", last_response.headers['Content-Type'] assert_match /my\ body/, last_response.body
But you may also want to... check just the mime type:
get '/myroute' assert_response_is_html
check for a 404 page:
get '/myroute' assert_response_not_found_html "Not found!"
This one does:
assert 404, last_response.status assert "test/html", last_response.headers['Content-Type'] assert_match /Not\ found!/, last_response.body
For all mime types in Rack::Mime you get 3 methods, where [shortcut] is the file extension
assert_response_[shortcut] # checks for status 200, content-type and body matching assert_response_is_[shortcut] # checks for content-type and status 200 assert_response_not_found_[shortcut] # checks for content-type, status 404 and body matching
You may define your own content-type with
. :my_shortcut, "text/mymimetype" # later... assert_response_my_shortcut "here the body" assert_response_is_my_shortcut assert_response_not_found_my_shortcut "Not found but correct content-type"
There is some additional sugar like
assert_response_redirect assert_response_raises assert_response_body assert_response_header assert_response_not_found assert_response_ok
Everything after assert_response_ is the called method of AssertResponse, look them up in the documentation.
When using the assert_response_ methods always the last_response object is checked.
If you want to check a saved response instead, you may use the little DSL
get '/myroute' my_response = last_response get '/other' assert_response my_response do is_html # checks content-type body 'my body' # same as 'body /my\ body/' end
Now inside the code block you may use the methods of AssertResponse (but without the assert_response_ prefixes).
Maybe add some sugar for cookie testing.
Contributing to assert-response
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
- Fork the project
- Start a feature/bugfix branch
- Commit and push until you are happy with your contribution
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright (c) 2011 Marc Rene Arns. See LICENSE.txt for further details.