1 require 'rexml/document'
3 module ActionController #:nodoc:
4 module Assertions #:nodoc:
5 module DeprecatedAssertions #:nodoc:
6 def assert_success(message=nil) #:nodoc:
7 assert_response(:success, message)
9 deprecate :assert_success => "use assert_response(:success)"
11 def assert_redirect(message=nil) #:nodoc:
12 assert_response(:redirect, message)
14 deprecate :assert_redirect => "use assert_response(:redirect)"
16 def assert_rendered_file(expected=nil, message=nil) #:nodoc:
17 assert_template(expected, message)
19 deprecate :assert_rendered_file => :assert_template
21 # ensure that the session has an object with the specified name
22 def assert_session_has(key=nil, message=nil) #:nodoc:
23 msg = build_message(message, "<?> is not in the session <?>", key, @response.session)
24 assert_block(msg) { @response.has_session_object?(key) }
26 deprecate :assert_session_has => "use assert(@response.has_session_object?(key))"
28 # ensure that the session has no object with the specified name
29 def assert_session_has_no(key=nil, message=nil) #:nodoc:
30 msg = build_message(message, "<?> is in the session <?>", key, @response.session)
31 assert_block(msg) { !@response.has_session_object?(key) }
33 deprecate :assert_session_has_no => "use assert(!@response.has_session_object?(key))"
35 def assert_session_equal(expected = nil, key = nil, message = nil) #:nodoc:
36 msg = build_message(message, "<?> expected in session['?'] but was <?>", expected, key, @response.session[key])
37 assert_block(msg) { expected == @response.session[key] }
39 deprecate :assert_session_equal => "use assert_equal(expected, @response[key])"
41 # -- cookie assertions ---------------------------------------------------
43 def assert_no_cookie(key = nil, message = nil) #:nodoc:
44 actual = @response.cookies[key]
45 msg = build_message(message, "<?> not expected in cookies['?']", actual, key)
46 assert_block(msg) { actual.nil? or actual.empty? }
48 deprecate :assert_no_cookie => "use assert(!@response.cookies.key?(key))"
50 def assert_cookie_equal(expected = nil, key = nil, message = nil) #:nodoc:
51 actual = @response.cookies[key]
52 actual = actual.first if actual
53 msg = build_message(message, "<?> expected in cookies['?'] but was <?>", expected, key, actual)
54 assert_block(msg) { expected == actual }
56 deprecate :assert_cookie_equal => "use assert(@response.cookies.key?(key))"
58 # -- flash assertions ---------------------------------------------------
60 # ensure that the flash has an object with the specified name
61 def assert_flash_has(key=nil, message=nil) #:nodoc:
62 msg = build_message(message, "<?> is not in the flash <?>", key, @response.flash)
63 assert_block(msg) { @response.has_flash_object?(key) }
65 deprecate :assert_flash_has => "use assert(@response.has_flash_object?(key))"
67 # ensure that the flash has no object with the specified name
68 def assert_flash_has_no(key=nil, message=nil) #:nodoc:
69 msg = build_message(message, "<?> is in the flash <?>", key, @response.flash)
70 assert_block(msg) { !@response.has_flash_object?(key) }
72 deprecate :assert_flash_has_no => "use assert(!@response.has_flash_object?(key))"
74 # ensure the flash exists
75 def assert_flash_exists(message=nil) #:nodoc:
76 msg = build_message(message, "the flash does not exist <?>", @response.session['flash'] )
77 assert_block(msg) { @response.has_flash? }
79 deprecate :assert_flash_exists => "use assert(@response.has_flash?)"
81 # ensure the flash does not exist
82 def assert_flash_not_exists(message=nil) #:nodoc:
83 msg = build_message(message, "the flash exists <?>", @response.flash)
84 assert_block(msg) { !@response.has_flash? }
86 deprecate :assert_flash_not_exists => "use assert(!@response.has_flash?)"
88 # ensure the flash is empty but existent
89 def assert_flash_empty(message=nil) #:nodoc:
90 msg = build_message(message, "the flash is not empty <?>", @response.flash)
91 assert_block(msg) { !@response.has_flash_with_contents? }
93 deprecate :assert_flash_empty => "use assert(!@response.has_flash_with_contents?)"
95 # ensure the flash is not empty
96 def assert_flash_not_empty(message=nil) #:nodoc:
97 msg = build_message(message, "the flash is empty")
98 assert_block(msg) { @response.has_flash_with_contents? }
100 deprecate :assert_flash_not_empty => "use assert(@response.has_flash_with_contents?)"
102 def assert_flash_equal(expected = nil, key = nil, message = nil) #:nodoc:
103 msg = build_message(message, "<?> expected in flash['?'] but was <?>", expected, key, @response.flash[key])
104 assert_block(msg) { expected == @response.flash[key] }
106 deprecate :assert_flash_equal => "use assert_equal(expected, @response.flash[key])"
109 # ensure our redirection url is an exact match
110 def assert_redirect_url(url=nil, message=nil) #:nodoc:
111 assert_redirect(message)
112 msg = build_message(message, "<?> is not the redirected location <?>", url, @response.redirect_url)
113 assert_block(msg) { @response.redirect_url == url }
115 deprecate :assert_redirect_url => "use assert_equal(url, @response.redirect_url)"
117 # ensure our redirection url matches a pattern
118 def assert_redirect_url_match(pattern=nil, message=nil) #:nodoc:
119 assert_redirect(message)
120 msg = build_message(message, "<?> was not found in the location: <?>", pattern, @response.redirect_url)
121 assert_block(msg) { @response.redirect_url_match?(pattern) }
123 deprecate :assert_redirect_url_match => "use assert(@response.redirect_url_match?(pattern))"
126 # -- template assertions ------------------------------------------------
128 # ensure that a template object with the given name exists
129 def assert_template_has(key=nil, message=nil) #:nodoc:
130 msg = build_message(message, "<?> is not a template object", key )
131 assert_block(msg) { @response.has_template_object?(key) }
133 deprecate :assert_template_has => "use assert(@response.has_template_object?(key))"
135 # ensure that a template object with the given name does not exist
136 def assert_template_has_no(key=nil,message=nil) #:nodoc:
137 msg = build_message(message, "<?> is a template object <?>", key, @response.template_objects[key])
138 assert_block(msg) { !@response.has_template_object?(key) }
140 deprecate :assert_template_has_no => "use assert(!@response.has_template_object?(key))"
142 # ensures that the object assigned to the template on +key+ is equal to +expected+ object.
143 def assert_template_equal(expected = nil, key = nil, message = nil) #:nodoc:
144 msg = build_message(message, "<?> expected in assigns['?'] but was <?>", expected, key, @response.template.assigns[key.to_s])
145 assert_block(msg) { expected == @response.template.assigns[key.to_s] }
147 alias_method :assert_assigned_equal, :assert_template_equal
148 deprecate :assert_assigned_equal => "use assert_equal(expected, @response.template.assigns[key.to_s])"
149 deprecate :assert_template_equal => "use assert_equal(expected, @response.template.assigns[key.to_s])"
151 # Asserts that the template returns the +expected+ string or array based on the XPath +expression+.
152 # This will only work if the template rendered a valid XML document.
153 def assert_template_xpath_match(expression=nil, expected=nil, message=nil) #:nodoc:
154 xml, matches = REXML::Document.new(@response.body), []
155 xml.elements.each(expression) { |e| matches << e.text }
156 if matches.empty? then
157 msg = build_message(message, "<?> not found in document", expression)
160 elsif matches.length < 2 then
161 matches = matches.first
164 msg = build_message(message, "<?> found <?>, not <?>", expression, matches, expected)
165 assert_block(msg) { matches == expected }
167 deprecate :assert_template_xpath_match => "you should use assert_tag, instead"
169 # Assert the template object with the given name is an Active Record descendant and is valid.
170 def assert_valid_record(key = nil, message = nil) #:nodoc:
171 record = find_record_in_template(key)
172 msg = build_message(message, "Active Record is invalid <?>)", record.errors.full_messages)
173 assert_block(msg) { record.valid? }
175 deprecate :assert_valid_record => "use assert(assigns(key).valid?)"
177 # Assert the template object with the given name is an Active Record descendant and is invalid.
178 def assert_invalid_record(key = nil, message = nil) #:nodoc:
179 record = find_record_in_template(key)
180 msg = build_message(message, "Active Record is valid)")
181 assert_block(msg) { !record.valid? }
183 deprecate :assert_invalid_record => "use assert(!assigns(key).valid?)"
185 # Assert the template object with the given name is an Active Record descendant and the specified column(s) are valid.
186 def assert_valid_column_on_record(key = nil, columns = "", message = nil) #:nodoc:
187 record = find_record_in_template(key)
188 record.send(:validate)
190 cols = glue_columns(columns)
191 cols.delete_if { |col| !record.errors.invalid?(col) }
192 msg = build_message(message, "Active Record has invalid columns <?>)", cols.join(",") )
193 assert_block(msg) { cols.empty? }
195 deprecate :assert_valid_column_on_record => "use assert(!record.errors.invalid?(column)) instead"
197 # Assert the template object with the given name is an Active Record descendant and the specified column(s) are invalid.
198 def assert_invalid_column_on_record(key = nil, columns = "", message = nil) #:nodoc:
199 record = find_record_in_template(key)
200 record.send(:validate)
202 cols = glue_columns(columns)
203 cols.delete_if { |col| record.errors.invalid?(col) }
204 msg = build_message(message, "Active Record has valid columns <?>)", cols.join(",") )
205 assert_block(msg) { cols.empty? }
207 deprecate :assert_invalid_column_on_record => "use assert(record.errors.invalid?(column)) instead"
210 def glue_columns(columns)
212 cols << columns if columns.class == String
213 cols += columns if columns.class == Array
217 def find_record_in_template(key = nil)
218 assert_not_nil assigns(key)
219 record = @response.template_objects[key]
221 assert_not_nil(record)
222 assert_kind_of ActiveRecord::Base, record