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