| Module | RefreshTo::TestHelpers |
| In: |
lib/refresh_to.rb
|
Similar to the Rails assert_redirected_to() test helper. Pass a URL or a route hash (e.g. :action => "index").
Example:
def test_sign_in_refresh
post :sign_in assert_refreshed_to '/my_profile'
end
# File lib/refresh_to.rb, line 77
77: def assert_refreshed_to(url = {})
78: match_data = @response.body.match(/<meta http-equiv="refresh" content="0;url=([\S]+)">/)
79: parts = match_data[1].split("http://test.host")
80: destination = parts.last
81: case url.class.to_s
82: when 'String'
83: parts = url.split("http://test.host")
84: url = parts.last
85: assert_block("expected a refresh to '#{url}', but refreshed to #{destination}.") do
86: destination == url
87: end
88: when 'Hash'
89: assert_refreshed_to(@controller.url_for(url))
90: else
91: raise ArgumentError, "expected a URL either as a String (e.g. '/users') or as a hash (e.g. :controller => 'users', :action => 'index'), but you passed a #{url.class.to_s}"
92: end
93: end
Similar to the Rails assert_response() test helper, but adds a :refresh option. If the response isn‘t a refresh, calls the default assert_response() helper with the argument passed. If you want to assert a :success, you should still use the assert_response_r() method, as the built-in assert_response() will return true for :success even if refresh_to() is called.
To do: monkey-patch the built-in Rails assert_response() method to accept :refresh.
Example:
def test_sign_in_refresh
post :sign_in assert_response_r :refresh
end
# File lib/refresh_to.rb, line 113
113: def assert_response_r(type, message = nil)
114: is_refresh = @response.body =~ /<html>\s*<head>\s*<meta http-equiv="refresh" content="0;url=.*\s*<\/head>\s*<\/html>/
115: if type == :refresh
116: assert_block("Expected response to be a refresh, but was #{@response.response_code}") do
117: #raise @response.body
118: is_refresh
119: end
120: elsif type == :success and is_refresh
121: assert_block("Expected response to be a success, but was a refresh") { false }
122: else
123: assert_response(type, message)
124: end
125:
126: end