cavalle-rspec-rails 1.2.2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. data/.document +7 -0
  2. data/History.rdoc +207 -0
  3. data/License.txt +33 -0
  4. data/Manifest.txt +167 -0
  5. data/README.rdoc +45 -0
  6. data/Rakefile +79 -0
  7. data/TODO.txt +1 -0
  8. data/Upgrade.rdoc +103 -0
  9. data/features/step_definitions/people.rb +6 -0
  10. data/features/support/env.rb +13 -0
  11. data/features/transactions/transactions_should_rollback.feature +16 -0
  12. data/generators/rspec/CHANGES +1 -0
  13. data/generators/rspec/rspec_generator.rb +54 -0
  14. data/generators/rspec/templates/previous_failures.txt +0 -0
  15. data/generators/rspec/templates/rcov.opts +2 -0
  16. data/generators/rspec/templates/rspec.rake +165 -0
  17. data/generators/rspec/templates/script/autospec +6 -0
  18. data/generators/rspec/templates/script/spec +10 -0
  19. data/generators/rspec/templates/script/spec_server +9 -0
  20. data/generators/rspec/templates/spec.opts +4 -0
  21. data/generators/rspec/templates/spec_helper.rb +47 -0
  22. data/generators/rspec_controller/USAGE +33 -0
  23. data/generators/rspec_controller/rspec_controller_generator.rb +45 -0
  24. data/generators/rspec_controller/templates/controller_spec.rb +25 -0
  25. data/generators/rspec_controller/templates/helper_spec.rb +11 -0
  26. data/generators/rspec_controller/templates/view_spec.rb +12 -0
  27. data/generators/rspec_default_values.rb +19 -0
  28. data/generators/rspec_model/USAGE +18 -0
  29. data/generators/rspec_model/rspec_model_generator.rb +35 -0
  30. data/generators/rspec_model/templates/model_spec.rb +15 -0
  31. data/generators/rspec_scaffold/rspec_scaffold_generator.rb +150 -0
  32. data/generators/rspec_scaffold/templates/controller_spec.rb +171 -0
  33. data/generators/rspec_scaffold/templates/edit_erb_spec.rb +27 -0
  34. data/generators/rspec_scaffold/templates/helper_spec.rb +11 -0
  35. data/generators/rspec_scaffold/templates/index_erb_spec.rb +28 -0
  36. data/generators/rspec_scaffold/templates/new_erb_spec.rb +27 -0
  37. data/generators/rspec_scaffold/templates/routing_spec.rb +63 -0
  38. data/generators/rspec_scaffold/templates/show_erb_spec.rb +23 -0
  39. data/init.rb +9 -0
  40. data/lib/autotest/discover.rb +1 -0
  41. data/lib/autotest/rails_rspec.rb +76 -0
  42. data/lib/spec/rails/example/assigns_hash_proxy.rb +39 -0
  43. data/lib/spec/rails/example/controller_example_group.rb +247 -0
  44. data/lib/spec/rails/example/cookies_proxy.rb +29 -0
  45. data/lib/spec/rails/example/functional_example_group.rb +84 -0
  46. data/lib/spec/rails/example/helper_example_group.rb +153 -0
  47. data/lib/spec/rails/example/model_example_group.rb +14 -0
  48. data/lib/spec/rails/example/render_observer.rb +67 -0
  49. data/lib/spec/rails/example/routing_example_group.rb +13 -0
  50. data/lib/spec/rails/example/routing_helpers.rb +70 -0
  51. data/lib/spec/rails/example/view_example_group.rb +186 -0
  52. data/lib/spec/rails/example.rb +47 -0
  53. data/lib/spec/rails/extensions/action_controller/rescue.rb +42 -0
  54. data/lib/spec/rails/extensions/action_controller/test_case.rb +16 -0
  55. data/lib/spec/rails/extensions/action_controller/test_response.rb +21 -0
  56. data/lib/spec/rails/extensions/action_view/base.rb +33 -0
  57. data/lib/spec/rails/extensions/active_record/base.rb +45 -0
  58. data/lib/spec/rails/extensions/active_support/test_case.rb +7 -0
  59. data/lib/spec/rails/extensions/spec/matchers/have.rb +23 -0
  60. data/lib/spec/rails/extensions/spec/runner/configuration.rb +44 -0
  61. data/lib/spec/rails/extensions.rb +11 -0
  62. data/lib/spec/rails/interop/testcase.rb +14 -0
  63. data/lib/spec/rails/matchers/ar_be_valid.rb +44 -0
  64. data/lib/spec/rails/matchers/assert_select.rb +146 -0
  65. data/lib/spec/rails/matchers/change.rb +11 -0
  66. data/lib/spec/rails/matchers/have_text.rb +57 -0
  67. data/lib/spec/rails/matchers/include_text.rb +54 -0
  68. data/lib/spec/rails/matchers/redirect_to.rb +126 -0
  69. data/lib/spec/rails/matchers/render_template.rb +114 -0
  70. data/lib/spec/rails/matchers.rb +32 -0
  71. data/lib/spec/rails/mocks.rb +135 -0
  72. data/lib/spec/rails/spec_server.rb +127 -0
  73. data/lib/spec/rails/story_adapter.rb +79 -0
  74. data/lib/spec/rails/version.rb +15 -0
  75. data/lib/spec/rails.rb +28 -0
  76. data/rspec-rails.gemspec +57 -0
  77. data/spec/autotest/mappings_spec.rb +86 -0
  78. data/spec/rails_suite.rb +7 -0
  79. data/spec/resources/controllers/action_view_base_spec_controller.rb +2 -0
  80. data/spec/resources/controllers/application.rb +9 -0
  81. data/spec/resources/controllers/controller_spec_controller.rb +120 -0
  82. data/spec/resources/controllers/example.txt +1 -0
  83. data/spec/resources/controllers/redirect_spec_controller.rb +70 -0
  84. data/spec/resources/controllers/render_spec_controller.rb +30 -0
  85. data/spec/resources/controllers/rjs_spec_controller.rb +58 -0
  86. data/spec/resources/helpers/addition_helper.rb +5 -0
  87. data/spec/resources/helpers/explicit_helper.rb +46 -0
  88. data/spec/resources/helpers/more_explicit_helper.rb +5 -0
  89. data/spec/resources/helpers/plugin_application_helper.rb +6 -0
  90. data/spec/resources/helpers/view_spec_helper.rb +13 -0
  91. data/spec/resources/models/animal.rb +4 -0
  92. data/spec/resources/models/person.rb +18 -0
  93. data/spec/resources/models/thing.rb +3 -0
  94. data/spec/resources/views/controller_spec/_partial.html.erb +0 -0
  95. data/spec/resources/views/controller_spec/action_setting_flash_after_session_reset.html.erb +1 -0
  96. data/spec/resources/views/controller_spec/action_setting_flash_before_session_reset.html.erb +1 -0
  97. data/spec/resources/views/controller_spec/action_setting_the_assigns_hash.html.erb +0 -0
  98. data/spec/resources/views/controller_spec/action_with_errors_in_template.html.erb +1 -0
  99. data/spec/resources/views/controller_spec/action_with_template.html.erb +1 -0
  100. data/spec/resources/views/layouts/application.html.erb +0 -0
  101. data/spec/resources/views/layouts/simple.html.erb +0 -0
  102. data/spec/resources/views/objects/_object.html.erb +1 -0
  103. data/spec/resources/views/render_spec/_a_partial.html.erb +0 -0
  104. data/spec/resources/views/render_spec/action_with_alternate_layout.html.erb +0 -0
  105. data/spec/resources/views/render_spec/some_action.html.erb +0 -0
  106. data/spec/resources/views/render_spec/some_action.js.rjs +1 -0
  107. data/spec/resources/views/rjs_spec/_replacement_partial.html.erb +1 -0
  108. data/spec/resources/views/rjs_spec/hide_div.js.rjs +1 -0
  109. data/spec/resources/views/rjs_spec/hide_page_element.js.rjs +1 -0
  110. data/spec/resources/views/rjs_spec/insert_html.js.rjs +1 -0
  111. data/spec/resources/views/rjs_spec/replace.js.rjs +1 -0
  112. data/spec/resources/views/rjs_spec/replace_html.js.rjs +1 -0
  113. data/spec/resources/views/rjs_spec/replace_html_with_partial.js.rjs +1 -0
  114. data/spec/resources/views/rjs_spec/visual_effect.js.rjs +1 -0
  115. data/spec/resources/views/rjs_spec/visual_toggle_effect.js.rjs +1 -0
  116. data/spec/resources/views/tag_spec/no_tags.html.erb +1 -0
  117. data/spec/resources/views/tag_spec/single_div_with_no_attributes.html.erb +1 -0
  118. data/spec/resources/views/tag_spec/single_div_with_one_attribute.html.erb +1 -0
  119. data/spec/resources/views/view_spec/_partial.html.erb +2 -0
  120. data/spec/resources/views/view_spec/_partial_used_twice.html.erb +0 -0
  121. data/spec/resources/views/view_spec/_partial_with_local_variable.html.erb +1 -0
  122. data/spec/resources/views/view_spec/_partial_with_sub_partial.html.erb +1 -0
  123. data/spec/resources/views/view_spec/_spacer.html.erb +1 -0
  124. data/spec/resources/views/view_spec/accessor.html.erb +5 -0
  125. data/spec/resources/views/view_spec/block_helper.html.erb +3 -0
  126. data/spec/resources/views/view_spec/entry_form.html.erb +2 -0
  127. data/spec/resources/views/view_spec/explicit_helper.html.erb +2 -0
  128. data/spec/resources/views/view_spec/foo/show.html.erb +1 -0
  129. data/spec/resources/views/view_spec/implicit_helper.html.erb +2 -0
  130. data/spec/resources/views/view_spec/multiple_helpers.html.erb +3 -0
  131. data/spec/resources/views/view_spec/path_params.html.erb +1 -0
  132. data/spec/resources/views/view_spec/should_not_receive.html.erb +3 -0
  133. data/spec/resources/views/view_spec/template_with_partial.html.erb +5 -0
  134. data/spec/resources/views/view_spec/template_with_partial_using_collection.html.erb +3 -0
  135. data/spec/resources/views/view_spec/template_with_partial_with_array.html.erb +1 -0
  136. data/spec/spec/rails/example/assigns_hash_proxy_spec.rb +109 -0
  137. data/spec/spec/rails/example/configuration_spec.rb +65 -0
  138. data/spec/spec/rails/example/controller_example_group_spec.rb +299 -0
  139. data/spec/spec/rails/example/controller_isolation_spec.rb +56 -0
  140. data/spec/spec/rails/example/cookies_proxy_spec.rb +87 -0
  141. data/spec/spec/rails/example/error_handling_spec.rb +90 -0
  142. data/spec/spec/rails/example/example_group_factory_spec.rb +112 -0
  143. data/spec/spec/rails/example/helper_example_group_spec.rb +233 -0
  144. data/spec/spec/rails/example/model_example_group_spec.rb +20 -0
  145. data/spec/spec/rails/example/routing_example_group_spec.rb +9 -0
  146. data/spec/spec/rails/example/shared_routing_example_group_examples.rb +44 -0
  147. data/spec/spec/rails/example/test_unit_assertion_accessibility_spec.rb +33 -0
  148. data/spec/spec/rails/example/view_example_group_spec.rb +335 -0
  149. data/spec/spec/rails/extensions/action_view_base_spec.rb +48 -0
  150. data/spec/spec/rails/extensions/active_record_spec.rb +14 -0
  151. data/spec/spec/rails/interop/testcase_spec.rb +70 -0
  152. data/spec/spec/rails/matchers/ar_be_valid_spec.rb +45 -0
  153. data/spec/spec/rails/matchers/assert_select_spec.rb +809 -0
  154. data/spec/spec/rails/matchers/errors_on_spec.rb +25 -0
  155. data/spec/spec/rails/matchers/have_text_spec.rb +70 -0
  156. data/spec/spec/rails/matchers/include_text_spec.rb +62 -0
  157. data/spec/spec/rails/matchers/redirect_to_spec.rb +253 -0
  158. data/spec/spec/rails/matchers/render_template_spec.rb +191 -0
  159. data/spec/spec/rails/matchers/should_change_spec.rb +15 -0
  160. data/spec/spec/rails/mocks/ar_classes.rb +10 -0
  161. data/spec/spec/rails/mocks/mock_model_spec.rb +106 -0
  162. data/spec/spec/rails/mocks/stub_model_spec.rb +80 -0
  163. data/spec/spec/rails/sample_modified_fixture.rb +8 -0
  164. data/spec/spec/rails/sample_spec.rb +8 -0
  165. data/spec/spec/rails/spec_server_spec.rb +107 -0
  166. data/spec/spec/rails/spec_spec.rb +11 -0
  167. data/spec/spec_helper.rb +79 -0
  168. metadata +277 -0
@@ -0,0 +1,809 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ # assert_select plugins for Rails
4
+ #
5
+ # Copyright (c) 2006 Assaf Arkin, under Creative Commons Attribution and/or MIT License
6
+ # Developed for http://co.mments.com
7
+ # Code and documention: http://labnotes.org
8
+
9
+ class AssertSelectController < ActionController::Base
10
+
11
+ def response=(content)
12
+ @content = content
13
+ end
14
+
15
+ def html()
16
+ render :text=>@content, :layout=>false, :content_type=>Mime::HTML
17
+ @content = nil
18
+ end
19
+
20
+ def rjs()
21
+ update = @update
22
+ render :update do |page|
23
+ update.call page
24
+ end
25
+ @update = nil
26
+ end
27
+
28
+ def xml()
29
+ render :text=>@content, :layout=>false, :content_type=>Mime::XML
30
+ @content = nil
31
+ end
32
+
33
+ end
34
+
35
+ class AssertSelectMailer < ActionMailer::Base
36
+
37
+ def test(html)
38
+ recipients "test <test@test.host>"
39
+ from "test@test.host"
40
+ subject "Test e-mail"
41
+ part :content_type=>"text/html", :body=>html
42
+ end
43
+
44
+ end
45
+
46
+ module AssertSelectSpecHelpers
47
+ def render_html(html)
48
+ @controller.response = html
49
+ get :html
50
+ end
51
+
52
+ def render_rjs(&block)
53
+ clear_response
54
+ @controller.response &block
55
+ get :rjs
56
+ end
57
+
58
+ def render_xml(xml)
59
+ @controller.response = xml
60
+ get :xml
61
+ end
62
+
63
+ def first_non_rspec_line_in_backtrace_of(error)
64
+ rlocation = File.join('rspec', 'lib', 'spec')
65
+ error.backtrace.reject { |line|
66
+ line =~ /#{rlocation}/
67
+ }.first
68
+ end
69
+
70
+ private
71
+ # necessary for 1.2.1
72
+ def clear_response
73
+ render_html("")
74
+ end
75
+ end
76
+
77
+ unless defined?(SpecFailed)
78
+ SpecFailed = Spec::Expectations::ExpectationNotMetError
79
+ end
80
+
81
+ describe "should have_tag", :type => :controller do
82
+ include AssertSelectSpecHelpers
83
+ controller_name :assert_select
84
+ integrate_views
85
+
86
+ it "should find specific numbers of elements" do
87
+ render_html %Q{<div id="1"></div><div id="2"></div>}
88
+ response.should have_tag( "div" )
89
+ response.should have_tag("div", 2)
90
+ lambda { response.should_not have_tag("div") }.should raise_error(SpecFailed, "should not have tag(\"div\"), but did")
91
+
92
+ lambda { response.should have_tag("div", 3) }.should raise_error(SpecFailed)
93
+ lambda { response.should have_tag("p") }.should raise_error(SpecFailed)
94
+ end
95
+
96
+ it "should expect to find elements when using true" do
97
+ render_html %Q{<div id="1"></div><div id="2"></div>}
98
+ response.should have_tag( "div", true )
99
+ lambda { response.should have_tag( "p", true )}.should raise_error(SpecFailed)
100
+ end
101
+
102
+ it "should expect to not find elements when using false" do
103
+ render_html %Q{<div id="1"></div><div id="2"></div>}
104
+ response.should have_tag( "p", false )
105
+ lambda { response.should have_tag( "div", false )}.should raise_error(SpecFailed)
106
+ end
107
+
108
+
109
+ it "should match submitted text using text or regexp" do
110
+ render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
111
+ response.should have_tag("div", "foo")
112
+ response.should have_tag("div", /(foo|bar)/)
113
+ response.should have_tag("div", :text=>"foo")
114
+ response.should have_tag("div", :text=>/(foo|bar)/)
115
+
116
+ lambda { response.should have_tag("div", "bar") }.should raise_error(SpecFailed)
117
+ lambda { response.should have_tag("div", :text=>"bar") }.should raise_error(SpecFailed)
118
+ lambda { response.should have_tag("p", :text=>"foo") }.should raise_error(SpecFailed)
119
+
120
+ lambda { response.should have_tag("div", /foobar/) }.should raise_error(SpecFailed)
121
+ lambda { response.should have_tag("div", :text=>/foobar/) }.should raise_error(SpecFailed)
122
+ lambda { response.should have_tag("p", :text=>/foo/) }.should raise_error(SpecFailed)
123
+ end
124
+
125
+ it "should use submitted message" do
126
+ render_html %Q{nothing here}
127
+ lambda {
128
+ response.should have_tag("div", {}, "custom message")
129
+ }.should raise_error(SpecFailed, /custom message/)
130
+ end
131
+
132
+ it "should match submitted html" do
133
+ render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
134
+ text = "\"This is not a big problem,\" he said."
135
+ html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
136
+ response.should have_tag("p", text)
137
+ lambda { response.should have_tag("p", html) }.should raise_error(SpecFailed)
138
+ response.should have_tag("p", :html=>html)
139
+ lambda { response.should have_tag("p", :html=>text) }.should raise_error(SpecFailed)
140
+
141
+ # # No stripping for pre.
142
+ render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
143
+ text = "\n\"This is not a big problem,\" he said.\n"
144
+ html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
145
+ response.should have_tag("pre", text)
146
+ lambda { response.should have_tag("pre", html) }.should raise_error(SpecFailed)
147
+ response.should have_tag("pre", :html=>html)
148
+ lambda { response.should have_tag("pre", :html=>text) }.should raise_error(SpecFailed)
149
+ end
150
+
151
+ it "should match number of instances" do
152
+ render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
153
+ response.should have_tag("div", 2)
154
+ lambda { response.should have_tag("div", 3) }.should raise_error(SpecFailed)
155
+ response.should have_tag("div", 1..2)
156
+ lambda { response.should have_tag("div", 3..4) }.should raise_error(SpecFailed)
157
+ response.should have_tag("div", :count=>2)
158
+ lambda { response.should have_tag("div", :count=>3) }.should raise_error(SpecFailed)
159
+ response.should have_tag("div", :minimum=>1)
160
+ response.should have_tag("div", :minimum=>2)
161
+ lambda { response.should have_tag("div", :minimum=>3) }.should raise_error(SpecFailed)
162
+ response.should have_tag("div", :maximum=>2)
163
+ response.should have_tag("div", :maximum=>3)
164
+ lambda { response.should have_tag("div", :maximum=>1) }.should raise_error(SpecFailed)
165
+ response.should have_tag("div", :minimum=>1, :maximum=>2)
166
+ lambda { response.should have_tag("div", :minimum=>3, :maximum=>4) }.should raise_error(SpecFailed)
167
+ end
168
+
169
+ it "substitution values" do
170
+ render_html %Q{<div id="1">foo</div><div id="2">foo</div><span id="3"></span>}
171
+ response.should have_tag("div#?", /\d+/) do |elements| #using do/end
172
+ elements.size.should == 2
173
+ end
174
+ response.should have_tag("div#?", /\d+/) { |elements| #using {}
175
+ elements.size.should == 2
176
+ }
177
+ lambda {
178
+ response.should have_tag("div#?", /\d+/) do |elements|
179
+ elements.size.should == 3
180
+ end
181
+ }.should raise_error(SpecFailed, "expected: 3,\n got: 2 (using ==)")
182
+
183
+ lambda {
184
+ response.should have_tag("div#?", /\d+/) { |elements|
185
+ elements.size.should == 3
186
+ }
187
+ }.should raise_error(SpecFailed, "expected: 3,\n got: 2 (using ==)")
188
+
189
+ response.should have_tag("div#?", /\d+/) do |elements|
190
+ elements.size.should == 2
191
+ with_tag("#1")
192
+ with_tag("#2")
193
+ without_tag("#3")
194
+ end
195
+ end
196
+
197
+ #added for RSpec
198
+ it "nested tags in form" do
199
+ render_html %Q{
200
+ <form action="test">
201
+ <input type="text" name="email">
202
+ </form>
203
+ <form action="other">
204
+ <input type="text" name="other_input">
205
+ </form>
206
+ }
207
+ response.should have_tag("form[action=test]") { |form|
208
+ with_tag("input[type=text][name=email]")
209
+ }
210
+ response.should have_tag("form[action=other]") { |form|
211
+ with_tag("input[type=text][name=other_input]")
212
+ }
213
+
214
+ lambda {
215
+ response.should have_tag("form[action=test]") { |form|
216
+ with_tag("input[type=text][name=other_input]")
217
+ }
218
+ }.should raise_error(SpecFailed)
219
+
220
+ lambda {
221
+ response.should have_tag("form[action=test]") {
222
+ with_tag("input[type=text][name=other_input]")
223
+ }
224
+ }.should raise_error(SpecFailed)
225
+ end
226
+
227
+ it "should report the correct line number for a nested failed expectation" do
228
+ render_html %Q{
229
+ <form action="test">
230
+ <input type="text" name="email">
231
+ </form>
232
+ }
233
+ begin
234
+ response.should have_tag("form[action=test]") {
235
+ @expected_error_line = __LINE__; should have_tag("input[type=text][name=other_input]")
236
+ }
237
+ rescue => e
238
+ first_non_rspec_line_in_backtrace_of(e).should =~
239
+ /#{File.basename(__FILE__)}:#{@expected_error_line}/
240
+ else
241
+ fail
242
+ end
243
+ end
244
+
245
+ it "should report the correct line number for a nested raised exception" do
246
+ render_html %Q{
247
+ <form action="test">
248
+ <input type="text" name="email">
249
+ </form>
250
+ }
251
+ begin
252
+ response.should have_tag("form[action=test]") {
253
+ @expected_error_line = __LINE__; raise "Failed!"
254
+ }
255
+ rescue => e
256
+ first_non_rspec_line_in_backtrace_of(e).should =~
257
+ /#{File.basename(__FILE__)}:#{@expected_error_line}/
258
+ else
259
+ fail
260
+ end
261
+ end
262
+
263
+ it "should report the correct line number for a nested failed test/unit assertion" do
264
+ pending "Doesn't work at the moment. Do we want to support this?" do
265
+ render_html %Q{
266
+ <form action="test">
267
+ <input type="text" name="email">
268
+ </form>
269
+ }
270
+ begin
271
+ response.should have_tag("form[action=test]") {
272
+ @expected_error_line = __LINE__; assert false
273
+ }
274
+ rescue => e
275
+ first_non_rspec_line_in_backtrace_of(e).should =~
276
+ /#{File.basename(__FILE__)}:#{@expected_error_line}/
277
+ else
278
+ fail
279
+ end
280
+ end
281
+ end
282
+
283
+
284
+ it "beatles" do
285
+ unless defined?(BEATLES)
286
+ BEATLES = [
287
+ ["John", "Guitar"],
288
+ ["George", "Guitar"],
289
+ ["Paul", "Bass"],
290
+ ["Ringo", "Drums"]
291
+ ]
292
+ end
293
+
294
+ render_html %Q{
295
+ <div id="beatles">
296
+ <div class="beatle">
297
+ <h2>John</h2><p>Guitar</p>
298
+ </div>
299
+ <div class="beatle">
300
+ <h2>George</h2><p>Guitar</p>
301
+ </div>
302
+ <div class="beatle">
303
+ <h2>Paul</h2><p>Bass</p>
304
+ </div>
305
+ <div class="beatle">
306
+ <h2>Ringo</h2><p>Drums</p>
307
+ </div>
308
+ </div>
309
+ }
310
+ response.should have_tag("div#beatles>div[class=\"beatle\"]", 4)
311
+
312
+ response.should have_tag("div#beatles>div.beatle") {
313
+ BEATLES.each { |name, instrument|
314
+ with_tag("div.beatle>h2", name)
315
+ with_tag("div.beatle>p", instrument)
316
+ without_tag("div.beatle>span")
317
+ }
318
+ }
319
+ end
320
+
321
+ it "assert_select_text_match" do
322
+ render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
323
+ response.should have_tag("div") do |divs|
324
+ with_tag("div", "foo")
325
+ with_tag("div", "bar")
326
+ with_tag("div", /\w*/)
327
+ with_tag("div", /\w*/, :count=>2)
328
+ without_tag("div", :text=>"foo", :count=>2)
329
+ with_tag("div", :html=>"<span>bar</span>")
330
+ with_tag("div", :html=>"<span>bar</span>")
331
+ with_tag("div", :html=>/\w*/)
332
+ with_tag("div", :html=>/\w*/, :count=>2)
333
+ without_tag("div", :html=>"<span>foo</span>", :count=>2)
334
+ end
335
+ end
336
+
337
+
338
+ it "assert_select_from_rjs with one item" do
339
+ render_rjs do |page|
340
+ page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
341
+ end
342
+ response.should have_tag("div") { |elements|
343
+ elements.size.should == 2
344
+ with_tag("#1")
345
+ with_tag("#2")
346
+ }
347
+
348
+ lambda {
349
+ response.should have_tag("div") { |elements|
350
+ elements.size.should == 2
351
+ with_tag("#1")
352
+ with_tag("#3")
353
+ }
354
+ }.should raise_error(SpecFailed)
355
+
356
+ lambda {
357
+ response.should have_tag("div") { |elements|
358
+ elements.size.should == 2
359
+ with_tag("#1")
360
+ without_tag("#2")
361
+ }
362
+ }.should raise_error(SpecFailed, "should not have tag(\"#2\"), but did")
363
+
364
+ lambda {
365
+ response.should have_tag("div") { |elements|
366
+ elements.size.should == 3
367
+ with_tag("#1")
368
+ with_tag("#2")
369
+ }
370
+ }.should raise_error(SpecFailed)
371
+
372
+
373
+ response.should have_tag("div#?", /\d+/) { |elements|
374
+ with_tag("#1")
375
+ with_tag("#2")
376
+ }
377
+ end
378
+
379
+ it "assert_select_from_rjs with multiple items" do
380
+ render_rjs do |page|
381
+ page.replace_html "test", "<div id=\"1\">foo</div>"
382
+ page.replace_html "test2", "<div id=\"2\">foo</div>"
383
+ end
384
+ response.should have_tag("div")
385
+ response.should have_tag("div") { |elements|
386
+ elements.size.should == 2
387
+ with_tag("#1")
388
+ with_tag("#2")
389
+ }
390
+
391
+ lambda {
392
+ response.should have_tag("div") { |elements|
393
+ with_tag("#3")
394
+ }
395
+ }.should raise_error(SpecFailed)
396
+ end
397
+ end
398
+
399
+ describe "css_select", :type => :controller do
400
+ include AssertSelectSpecHelpers
401
+ controller_name :assert_select
402
+ integrate_views
403
+
404
+ it "can select tags from html" do
405
+ render_html %Q{<div id="1"></div><div id="2"></div>}
406
+ css_select("div").size.should == 2
407
+ css_select("p").size.should == 0
408
+ end
409
+
410
+
411
+ it "can select nested tags from html" do
412
+ render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
413
+ response.should have_tag("div#?", /\d+/) { |elements|
414
+ css_select(elements[0], "div").should have(1).element
415
+ css_select(elements[1], "div").should have(1).element
416
+ }
417
+ response.should have_tag("div") {
418
+ css_select("div").should have(2).elements
419
+ css_select("div").each { |element|
420
+ # Testing as a group is one thing
421
+ css_select("#1,#2").should have(2).elements
422
+ # Testing individually is another
423
+ css_select("#1").should have(1).element
424
+ css_select("#2").should have(1).element
425
+ }
426
+ }
427
+ end
428
+
429
+ it "can select nested tags from rjs (one result)" do
430
+ render_rjs do |page|
431
+ page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
432
+ end
433
+ css_select("div").should have(2).elements
434
+ css_select("#1").should have(1).element
435
+ css_select("#2").should have(1).element
436
+ end
437
+
438
+ it "can select nested tags from rjs (two results)" do
439
+ render_rjs do |page|
440
+ page.replace_html "test", "<div id=\"1\">foo</div>"
441
+ page.replace_html "test2", "<div id=\"2\">foo</div>"
442
+ end
443
+ css_select("div").should have(2).elements
444
+ css_select("#1").should have(1).element
445
+ css_select("#2").should have(1).element
446
+ end
447
+
448
+ end
449
+
450
+ describe "have_rjs behaviour_type", :type => :controller do
451
+ include AssertSelectSpecHelpers
452
+ controller_name :assert_select
453
+ integrate_views
454
+
455
+ before(:each) do
456
+ render_rjs do |page|
457
+ page.replace "test1", "<div id=\"1\">foo</div>"
458
+ page.replace_html "test2", "<div id=\"2\">bar</div><div id=\"3\">none</div>"
459
+ page.insert_html :top, "test3", "<div id=\"4\">loopy</div>"
460
+ page.hide "test4"
461
+ page["test5"].hide
462
+ end
463
+ end
464
+
465
+ it "should pass if any rjs exists" do
466
+ response.should have_rjs
467
+ end
468
+
469
+ it "should fail if no rjs exists" do
470
+ render_rjs do |page|
471
+ end
472
+ lambda do
473
+ response.should have_rjs
474
+ end.should raise_error(SpecFailed)
475
+ end
476
+
477
+ it "should find all rjs from multiple statements" do
478
+ response.should have_rjs do
479
+ with_tag("#1")
480
+ with_tag("#2")
481
+ with_tag("#3")
482
+ with_tag("#4")
483
+ end
484
+ end
485
+
486
+ it "should find by id" do
487
+ response.should have_rjs("test1") { |rjs|
488
+ rjs.size.should == 1
489
+ with_tag("div", 1)
490
+ with_tag("div#1", "foo")
491
+ }
492
+
493
+ lambda do
494
+ response.should have_rjs("test1") { |rjs|
495
+ rjs.size.should == 1
496
+ without_tag("div#1", "foo")
497
+ }
498
+ end.should raise_error(SpecFailed, "should not have tag(\"div#1\", \"foo\"), but did")
499
+
500
+ response.should have_rjs("test2") { |rjs|
501
+ rjs.size.should == 2
502
+ with_tag("div", 2)
503
+ with_tag("div#2", "bar")
504
+ with_tag("div#3", "none")
505
+ }
506
+ # response.should have_rjs("test4")
507
+ # response.should have_rjs("test5")
508
+ end
509
+
510
+ # specify "should find rjs using :hide" do
511
+ # response.should have_rjs(:hide)
512
+ # response.should have_rjs(:hide, "test4")
513
+ # response.should have_rjs(:hide, "test5")
514
+ # lambda do
515
+ # response.should have_rjs(:hide, "test3")
516
+ # end.should raise_error(SpecFailed)
517
+ # end
518
+
519
+ it "should find rjs using :replace" do
520
+ response.should have_rjs(:replace) { |rjs|
521
+ with_tag("div", 1)
522
+ with_tag("div#1", "foo")
523
+ }
524
+ response.should have_rjs(:replace, "test1") { |rjs|
525
+ with_tag("div", 1)
526
+ with_tag("div#1", "foo")
527
+ }
528
+ lambda {
529
+ response.should have_rjs(:replace, "test2")
530
+ }.should raise_error(SpecFailed)
531
+
532
+ lambda {
533
+ response.should have_rjs(:replace, "test3")
534
+ }.should raise_error(SpecFailed)
535
+ end
536
+
537
+ it "should find rjs using :replace_html" do
538
+ response.should have_rjs(:replace_html) { |rjs|
539
+ with_tag("div", 2)
540
+ with_tag("div#2", "bar")
541
+ with_tag("div#3", "none")
542
+ }
543
+
544
+ response.should have_rjs(:replace_html, "test2") { |rjs|
545
+ with_tag("div", 2)
546
+ with_tag("div#2", "bar")
547
+ with_tag("div#3", "none")
548
+ }
549
+
550
+ lambda {
551
+ response.should have_rjs(:replace_html, "test1")
552
+ }.should raise_error(SpecFailed)
553
+
554
+ lambda {
555
+ response.should have_rjs(:replace_html, "test3")
556
+ }.should raise_error(SpecFailed)
557
+ end
558
+
559
+ it "should find rjs using :insert_html (non-positioned)" do
560
+ response.should have_rjs(:insert_html) { |rjs|
561
+ with_tag("div", 1)
562
+ with_tag("div#4", "loopy")
563
+ }
564
+
565
+ response.should have_rjs(:insert_html, "test3") { |rjs|
566
+ with_tag("div", 1)
567
+ with_tag("div#4", "loopy")
568
+ }
569
+
570
+ lambda {
571
+ response.should have_rjs(:insert_html, "test1")
572
+ }.should raise_error(SpecFailed)
573
+
574
+ lambda {
575
+ response.should have_rjs(:insert_html, "test2")
576
+ }.should raise_error(SpecFailed)
577
+ end
578
+
579
+ it "should find rjs using :insert (positioned)" do
580
+ render_rjs do |page|
581
+ page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
582
+ page.insert_html :bottom, "test2", "<div id=\"2\">bar</div>"
583
+ page.insert_html :before, "test3", "<div id=\"3\">none</div>"
584
+ page.insert_html :after, "test4", "<div id=\"4\">loopy</div>"
585
+ end
586
+ response.should have_rjs(:insert, :top) do
587
+ with_tag("div", 1)
588
+ with_tag("#1")
589
+ end
590
+ response.should have_rjs(:insert, :top, "test1") do
591
+ with_tag("div", 1)
592
+ with_tag("#1")
593
+ end
594
+ response.should have_rjs(:insert, :bottom) {|rjs|
595
+ with_tag("div", 1)
596
+ with_tag("#2")
597
+ }
598
+ response.should have_rjs(:insert, :bottom, "test2") {|rjs|
599
+ with_tag("div", 1)
600
+ with_tag("#2")
601
+ }
602
+ response.should have_rjs(:insert, :before) {|rjs|
603
+ with_tag("div", 1)
604
+ with_tag("#3")
605
+ }
606
+ response.should have_rjs(:insert, :before, "test3") {|rjs|
607
+ with_tag("div", 1)
608
+ with_tag("#3")
609
+ }
610
+ response.should have_rjs(:insert, :after) {|rjs|
611
+ with_tag("div", 1)
612
+ with_tag("#4")
613
+ }
614
+ response.should have_rjs(:insert, :after, "test4") {|rjs|
615
+ with_tag("div", 1)
616
+ with_tag("#4")
617
+ }
618
+ end
619
+
620
+ it "should find rjs using :insert (positioned)" do
621
+ pending("await fix for http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/982")
622
+ render_rjs do |page|
623
+ page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
624
+ page.insert_html :bottom, "test2", "<div id=\"2\">bar</div>"
625
+ end
626
+ lambda {
627
+ response.should have_rjs(:insert, :top, "test2")
628
+ }.should raise_error(SpecFailed)
629
+ end
630
+ end
631
+
632
+ describe "send_email behaviour_type", :type => :controller do
633
+ include AssertSelectSpecHelpers
634
+ controller_name :assert_select
635
+ integrate_views
636
+
637
+ before(:each) do
638
+ ActionMailer::Base.delivery_method = :test
639
+ ActionMailer::Base.perform_deliveries = true
640
+ ActionMailer::Base.deliveries = []
641
+ end
642
+
643
+ after(:each) do
644
+ ActionMailer::Base.deliveries.clear
645
+ end
646
+
647
+ it "should fail with nothing sent" do
648
+ response.should_not send_email
649
+ lambda {
650
+ response.should send_email{}
651
+ }.should raise_error(SpecFailed, /No e-mail in delivery list./)
652
+ end
653
+
654
+ it "should pass otherwise" do
655
+ AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>"
656
+ response.should send_email
657
+ lambda {
658
+ response.should_not send_email
659
+ }.should raise_error(SpecFailed)
660
+ response.should send_email{}
661
+ response.should send_email {
662
+ with_tag("div:root") {
663
+ with_tag("p:first-child", "foo")
664
+ with_tag("p:last-child", "bar")
665
+ }
666
+ }
667
+
668
+ lambda {
669
+ response.should_not send_email
670
+ }.should raise_error(SpecFailed, "should not send email, but did")
671
+ end
672
+
673
+ end
674
+
675
+ describe "string.should have_tag", :type => :helper do
676
+ include AssertSelectSpecHelpers
677
+
678
+ it "should find root element" do
679
+ "<p>a paragraph</p>".should have_tag("p", "a paragraph")
680
+ end
681
+
682
+ it "should not find non-existent element" do
683
+ lambda do
684
+ "<p>a paragraph</p>".should have_tag("p", "wrong text")
685
+ end.should raise_error(SpecFailed)
686
+ end
687
+
688
+ it "should find child element" do
689
+ "<div><p>a paragraph</p></div>".should have_tag("p", "a paragraph")
690
+ end
691
+
692
+ it "should find nested element in one line" do
693
+ "<div><p>a paragraph</p></div>".should have_tag("div p","a paragraph")
694
+ end
695
+
696
+ it "should find nested element" do
697
+ "<div><p>a paragraph</p></div>".should have_tag("div") do
698
+ with_tag("p", "a paragraph")
699
+ end
700
+ end
701
+
702
+ it "should not find wrong nested element" do
703
+ lambda do
704
+ "<div><p>a paragraph</p></div>".should have_tag("div") do
705
+ with_tag("p", "wrong text")
706
+ end
707
+ end.should raise_error(SpecFailed)
708
+ end
709
+ end
710
+
711
+ describe "have_tag", :type => :controller do
712
+ include AssertSelectSpecHelpers
713
+ controller_name :assert_select
714
+ integrate_views
715
+
716
+ it "should work exactly the same as assert_select" do
717
+ render_html %Q{
718
+ <div id="wrapper">foo
719
+ <div class="piece">
720
+ <h3>Text</h3>
721
+ </div>
722
+ <div class="piece">
723
+ <h3>Another</h3>
724
+ </div>
725
+ </div>
726
+ }
727
+
728
+ assert_select "#wrapper .piece h3", :text => "Text"
729
+ assert_select "#wrapper .piece h3", :text => "Another"
730
+
731
+ response.should have_tag("#wrapper .piece h3", :text => "Text")
732
+ response.should have_tag("#wrapper .piece h3", :text => "Another")
733
+ end
734
+ end
735
+
736
+ describe 'selecting in HTML that contains a mock with null_object' do
737
+ include ActionController::Assertions::SelectorAssertions
738
+ module HTML
739
+ class Document
740
+ def initialize_with_strict_error_checking(text, strict=false, xml=false)
741
+ initialize_without_strict_error_checking(text, true, xml)
742
+ end
743
+ alias_method :initialize_without_strict_error_checking, :initialize
744
+ alias_method :initialize, :initialize_with_strict_error_checking
745
+ end
746
+ end
747
+
748
+ describe 'modified HTML::Document' do
749
+ it 'should raise error on valid HTML even though false is specified' do
750
+ lambda {HTML::Document.new("<b>#<Spec::Mocks::Mock:0x267b4f0></b>", false, false)}.should raise_error
751
+ end
752
+ end
753
+
754
+ it 'should not print errors from assert_select' do
755
+ mock = mock("Dog", :null_object => true)
756
+ html = "<b>#{mock.colour}</b>"
757
+ lambda {html.should have_tag('b')}.should_not raise_error
758
+ end
759
+ end
760
+
761
+ # describe "An rjs call to :visual_effect, a 'should have_rjs' spec with",
762
+ # :type => :view do
763
+ #
764
+ # before do
765
+ # render 'rjs_spec/visual_effect'
766
+ # end
767
+ #
768
+ # it "should pass with the correct element name" do
769
+ # response.should have_rjs(:effect, :fade, 'mydiv')
770
+ # end
771
+ #
772
+ # it "should fail the wrong element name" do
773
+ # lambda {
774
+ # response.should have_rjs(:effect, :fade, 'wrongname')
775
+ # }.should raise_error(SpecFailed)
776
+ # end
777
+ #
778
+ # it "should fail with the correct element but the wrong command" do
779
+ # lambda {
780
+ # response.should have_rjs(:effect, :puff, 'mydiv')
781
+ # }.should raise_error(SpecFailed)
782
+ # end
783
+ #
784
+ # end
785
+ #
786
+ # describe "An rjs call to :visual_effect for a toggle, a 'should have_rjs' spec with",
787
+ # :type => :view do
788
+ #
789
+ # before do
790
+ # render 'rjs_spec/visual_toggle_effect'
791
+ # end
792
+ #
793
+ # it "should pass with the correct element name" do
794
+ # response.should have_rjs(:effect, :toggle_blind, 'mydiv')
795
+ # end
796
+ #
797
+ # it "should fail with the wrong element name" do
798
+ # lambda {
799
+ # response.should have_rjs(:effect, :toggle_blind, 'wrongname')
800
+ # }.should raise_error(SpecFailed)
801
+ # end
802
+ #
803
+ # it "should fail the correct element but the wrong command" do
804
+ # lambda {
805
+ # response.should have_rjs(:effect, :puff, 'mydiv')
806
+ # }.should raise_error(SpecFailed)
807
+ # end
808
+ #
809
+ # end