Module: Test::Unit::Assertions
- Defined in:
- lib/action_controller/assertions/action_pack_assertions.rb,
lib/action_controller/assertions/active_record_assertions.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#acquire_assertion_target ⇒ Object
get the TestResponse object that these assertions depend upon.
-
#assert_assigned_equal(expected = nil, key = nil, message = nil) ⇒ Object
ensures that the object assigned to the template on
key
is equal toexpected
object. -
#assert_cookie_equal(expected = nil, key = nil, message = nil) ⇒ Object
– cookie assertions —————————————————.
-
#assert_flash_empty(message = nil) ⇒ Object
ensure the flash is empty but existent.
- #assert_flash_equal(expected = nil, key = nil, message = nil) ⇒ Object
-
#assert_flash_exists(message = nil) ⇒ Object
ensure the flash exists.
-
#assert_flash_has(key = nil, message = nil) ⇒ Object
ensure that the flash has an object with the specified name.
-
#assert_flash_has_no(key = nil, message = nil) ⇒ Object
ensure that the flash has no object with the specified name.
-
#assert_flash_not_empty(message = nil) ⇒ Object
ensure the flash is not empty.
-
#assert_flash_not_exists(message = nil) ⇒ Object
ensure the flash does not exist.
-
#assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that the provided options can be used to generate the provided path.
-
#assert_invalid_column_on_record(key = nil, columns = "", message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and the specified column(s) are invalid.
-
#assert_invalid_record(key = nil, message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and is invalid.
-
#assert_recognizes(expected_options, path, extras = {}, message = nil) ⇒ Object
Asserts that the routing of the given path is handled correctly and that the parsed options match.
-
#assert_redirect(message = nil) ⇒ Object
ensure we have be redirected.
-
#assert_redirect_url(url = nil, message = nil) ⇒ Object
ensure our redirection url is an exact match.
-
#assert_redirect_url_match(pattern = nil, message = nil) ⇒ Object
ensure our redirection url matches a pattern.
- #assert_redirected_to(options = {}, message = nil) ⇒ Object
-
#assert_rendered_file(expected = nil, message = nil) ⇒ Object
ensure the request was rendered with the appropriate template file.
-
#assert_routing(path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
asserts that path and options match both ways, in other words, the URL generated from options is same as path, and also that the options recognized from path are same as options.
- #assert_session_equal(expected = nil, key = nil, message = nil) ⇒ Object
-
#assert_session_has(key = nil, message = nil) ⇒ Object
ensure that the session has an object with the specified name.
-
#assert_session_has_no(key = nil, message = nil) ⇒ Object
ensure that the session has no object with the specified name.
-
#assert_success(message = nil) ⇒ Object
ensure that the web request has been serviced correctly.
-
#assert_template_has(key = nil, message = nil) ⇒ Object
ensure that a template object with the given name exists.
-
#assert_template_has_no(key = nil, message = nil) ⇒ Object
ensure that a template object with the given name does not exist.
-
#assert_template_xpath_match(expression = nil, expected = nil, message = nil) ⇒ Object
Asserts that the template returns the
expected
string or array based on the XPathexpression
. -
#assert_valid_column_on_record(key = nil, columns = "", message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and the specified column(s) are valid.
-
#assert_valid_record(key = nil, message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and is valid.
Instance Method Details
#acquire_assertion_target ⇒ Object
get the TestResponse object that these assertions depend upon
252 253 254 255 256 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 252 def acquire_assertion_target target = ActionController::TestResponse.assertion_target assert_block( "Unable to acquire the TestResponse.assertion_target. Please set this before calling this assertion." ) { !target.nil? } target end |
#assert_assigned_equal(expected = nil, key = nil, message = nil) ⇒ Object
ensures that the object assigned to the template on key
is equal to expected
object.
225 226 227 228 229 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 225 def assert_assigned_equal(expected = nil, key = nil, = nil) response = acquire_assertion_target msg = (, "<?> expected in assigns['?'] but was <?>", expected, key, response.template.assigns[key.to_s]) assert_block(msg) { expected == response.template.assigns[key.to_s] } end |
#assert_cookie_equal(expected = nil, key = nil, message = nil) ⇒ Object
– cookie assertions —————————————————
66 67 68 69 70 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 66 def (expected = nil, key = nil, = nil) response = acquire_assertion_target msg = (, "<?> expected in cookies['?'] but was <?>", expected, key, response.[key.to_s].first) assert_block(msg) { expected == response.[key.to_s].first } end |
#assert_flash_empty(message = nil) ⇒ Object
ensure the flash is empty but existent
103 104 105 106 107 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 103 def assert_flash_empty(=nil) response = acquire_assertion_target msg = (, "the flash is not empty <?>", response.flash) assert_block(msg) { !response.has_flash_with_contents? } end |
#assert_flash_equal(expected = nil, key = nil, message = nil) ⇒ Object
116 117 118 119 120 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 116 def assert_flash_equal(expected = nil, key = nil, = nil) response = acquire_assertion_target msg = (, "<?> expected in flash['?'] but was <?>", expected, key, response.flash[key]) assert_block(msg) { expected == response.flash[key] } end |
#assert_flash_exists(message = nil) ⇒ Object
ensure the flash exists
89 90 91 92 93 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 89 def assert_flash_exists(=nil) response = acquire_assertion_target msg = (, "the flash does not exist <?>", response.session['flash'] ) assert_block(msg) { response.has_flash? } end |
#assert_flash_has(key = nil, message = nil) ⇒ Object
ensure that the flash has an object with the specified name
75 76 77 78 79 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 75 def assert_flash_has(key=nil, =nil) response = acquire_assertion_target msg = (, "<?> is not in the flash <?>", key, response.flash) assert_block(msg) { response.has_flash_object?(key) } end |
#assert_flash_has_no(key = nil, message = nil) ⇒ Object
ensure that the flash has no object with the specified name
82 83 84 85 86 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 82 def assert_flash_has_no(key=nil, =nil) response = acquire_assertion_target msg = (, "<?> is in the flash <?>", key, response.flash) assert_block(msg) { !response.has_flash_object?(key) } end |
#assert_flash_not_empty(message = nil) ⇒ Object
ensure the flash is not empty
110 111 112 113 114 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 110 def assert_flash_not_empty(=nil) response = acquire_assertion_target msg = (, "the flash is empty") assert_block(msg) { response.has_flash_with_contents? } end |
#assert_flash_not_exists(message = nil) ⇒ Object
ensure the flash does not exist
96 97 98 99 100 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 96 def assert_flash_not_exists(=nil) response = acquire_assertion_target msg = (, "the flash exists <?>", response.flash) assert_block(msg) { !response.has_flash? } end |
#assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
Asserts that the provided options can be used to generate the provided path.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 182 def assert_generates(expected_path, , defaults={}, extras = {}, =nil) # Load routes.rb if it hasn't been loaded. ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? # Assume given controller request = ActionController::TestRequest.new({}, {}, nil) request.path_parameters = (defaults or {}).clone request.path_parameters[:controller] ||= [:controller] generated_path, found_extras = ActionController::Routing::Routes.generate(, request) generated_path = generated_path.join('/') msg = (, "found extras <?>, not <?>", found_extras, extras) assert_block(msg) { found_extras == extras } msg = (, "The generated path <?> did not match <?>", generated_path, expected_path) assert_block(msg) { expected_path == generated_path } end |
#assert_invalid_column_on_record(key = nil, columns = "", message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and the specified column(s) are invalid.
34 35 36 37 38 39 40 41 42 |
# File 'lib/action_controller/assertions/active_record_assertions.rb', line 34 def assert_invalid_column_on_record(key = nil, columns = "", = nil) record = find_record_in_template(key) record.send(:validate) cols = glue_columns(columns) cols.delete_if { |col| record.errors.invalid?(col) } msg = (, "Active Record has valid columns <?>)", cols.join(",") ) assert_block(msg) { cols.empty? } end |
#assert_invalid_record(key = nil, message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and is invalid.
16 17 18 19 20 |
# File 'lib/action_controller/assertions/active_record_assertions.rb', line 16 def assert_invalid_record(key = nil, = nil) record = find_record_in_template(key) msg = (, "Active Record is valid)") assert_block(msg) { !record.valid? } end |
#assert_recognizes(expected_options, path, extras = {}, message = nil) ⇒ Object
Asserts that the routing of the given path is handled correctly and that the parsed options match.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 164 def assert_recognizes(, path, extras={}, =nil) # Load routes.rb if it hasn't been loaded. ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? # Assume given controller request = ActionController::TestRequest.new({}, {}, nil) request.path = path ActionController::Routing::Routes.recognize!(request) = .clone extras.each_key { |key| .delete key } unless extras.nil? msg = (, "The recognized options <?> did not match <?>", request.path_parameters, ) assert_block(msg) { request.path_parameters == } end |
#assert_redirect(message = nil) ⇒ Object
ensure we have be redirected
125 126 127 128 129 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 125 def assert_redirect(=nil) response = acquire_assertion_target msg = (, "response is not a redirection (response code is <?>)", response.response_code) assert_block(msg) { response.redirect? } end |
#assert_redirect_url(url = nil, message = nil) ⇒ Object
ensure our redirection url is an exact match
146 147 148 149 150 151 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 146 def assert_redirect_url(url=nil, =nil) assert_redirect() response = acquire_assertion_target msg = (, "<?> is not the redirected location <?>", url, response.redirect_url) assert_block(msg) { response.redirect_url == url } end |
#assert_redirect_url_match(pattern = nil, message = nil) ⇒ Object
ensure our redirection url matches a pattern
154 155 156 157 158 159 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 154 def assert_redirect_url_match(pattern=nil, =nil) assert_redirect() response = acquire_assertion_target msg = (, "<?> was not found in the location: <?>", pattern, response.redirect_url) assert_block(msg) { response.redirect_url_match?(pattern) } end |
#assert_redirected_to(options = {}, message = nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 131 def assert_redirected_to( = {}, =nil) assert_redirect() response = acquire_assertion_target msg = (, "response is not a redirection to all of the options supplied (redirection is <?>)", response.redirected_to) assert_block(msg) do if .is_a?(Symbol) response.redirected_to == else .keys.all? { |k| [k] == ( response.redirected_to[k].respond_to?(:to_param) ? response.redirected_to[k].to_param : response.redirected_to[k] if response.redirected_to[k] ) } end end end |
#assert_rendered_file(expected = nil, message = nil) ⇒ Object
ensure the request was rendered with the appropriate template file
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 29 def assert_rendered_file(expected=nil, =nil) response = acquire_assertion_target rendered = expected ? response.rendered_file(!expected.include?('/')) : response.rendered_file msg = (, "expecting <?> but rendering with <?>", expected, rendered) assert_block(msg) do if expected.nil? response.rendered_with_file? else expected == rendered end end end |
#assert_routing(path, options, defaults = {}, extras = {}, message = nil) ⇒ Object
asserts that path and options match both ways, in other words, the URL generated from options is same as path, and also that the options recognized from path are same as options
203 204 205 206 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 203 def assert_routing(path, , defaults={}, extras={}, =nil) assert_recognizes(, path, extras, ) assert_generates(path, , defaults, extras, ) end |
#assert_session_equal(expected = nil, key = nil, message = nil) ⇒ Object
58 59 60 61 62 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 58 def assert_session_equal(expected = nil, key = nil, = nil) response = acquire_assertion_target msg = (, "<?> expected in session['?'] but was <?>", expected, key, response.session[key]) assert_block(msg) { expected == response.session[key] } end |
#assert_session_has(key = nil, message = nil) ⇒ Object
ensure that the session has an object with the specified name
45 46 47 48 49 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 45 def assert_session_has(key=nil, =nil) response = acquire_assertion_target msg = (, "<?> is not in the session <?>", key, response.session) assert_block(msg) { response.has_session_object?(key) } end |
#assert_session_has_no(key = nil, message = nil) ⇒ Object
ensure that the session has no object with the specified name
52 53 54 55 56 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 52 def assert_session_has_no(key=nil, =nil) response = acquire_assertion_target msg = (, "<?> is in the session <?>", key, response.session) assert_block(msg) { !response.has_session_object?(key) } end |
#assert_success(message = nil) ⇒ Object
ensure that the web request has been serviced correctly
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 12 def assert_success(=nil) response = acquire_assertion_target if response.success? # to count the assertion assert_block("") { true } else if response.redirect? msg = (, "Response unexpectedly redirect to <?>", response.redirect_url) else msg = (, "unsuccessful request (response code = <?>)", response.response_code) end assert_block(msg) { false } end end |
#assert_template_has(key = nil, message = nil) ⇒ Object
ensure that a template object with the given name exists
211 212 213 214 215 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 211 def assert_template_has(key=nil, =nil) response = acquire_assertion_target msg = (, "<?> is not a template object", key ) assert_block(msg) { response.has_template_object?(key) } end |
#assert_template_has_no(key = nil, message = nil) ⇒ Object
ensure that a template object with the given name does not exist
218 219 220 221 222 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 218 def assert_template_has_no(key=nil,=nil) response = acquire_assertion_target msg = (, "<?> is a template object <?>", key, response.template_objects[key]) assert_block(msg) { !response.has_template_object?(key) } end |
#assert_template_xpath_match(expression = nil, expected = nil, message = nil) ⇒ Object
Asserts that the template returns the expected
string or array based on the XPath expression
. This will only work if the template rendered a valid XML document.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/action_controller/assertions/action_pack_assertions.rb', line 233 def assert_template_xpath_match(expression=nil, expected=nil, =nil) response = acquire_assertion_target xml, matches = REXML::Document.new(response.body), [] xml.elements.each(expression) { |e| matches << e.text } if matches.empty? then msg = (, "<?> not found in document", expression) flunk(msg) return elsif matches.length < 2 then matches = matches.first end msg = (, "<?> found <?>, not <?>", expression, matches, expected) assert_block(msg) { matches == expected } end |
#assert_valid_column_on_record(key = nil, columns = "", message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and the specified column(s) are valid.
23 24 25 26 27 28 29 30 31 |
# File 'lib/action_controller/assertions/active_record_assertions.rb', line 23 def assert_valid_column_on_record(key = nil, columns = "", = nil) record = find_record_in_template(key) record.send(:validate) cols = glue_columns(columns) cols.delete_if { |col| !record.errors.invalid?(col) } msg = (, "Active Record has invalid columns <?>)", cols.join(",") ) assert_block(msg) { cols.empty? } end |
#assert_valid_record(key = nil, message = nil) ⇒ Object
Assert the template object with the given name is an Active Record descendant and is valid.
9 10 11 12 13 |
# File 'lib/action_controller/assertions/active_record_assertions.rb', line 9 def assert_valid_record(key = nil, = nil) record = find_record_in_template(key) msg = (, "Active Record is invalid <?>)", record.errors.) assert_block(msg) { record.valid? } end |