shoulda-matchers 2.7.0 → 2.8.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +13 -3
  3. data/Appraisals +18 -0
  4. data/CONTRIBUTING.md +13 -29
  5. data/Gemfile +1 -4
  6. data/Gemfile.lock +2 -10
  7. data/NEWS.md +100 -14
  8. data/README.md +62 -58
  9. data/Rakefile +9 -16
  10. data/gemfiles/3.0.gemfile +11 -12
  11. data/gemfiles/3.0.gemfile.lock +15 -10
  12. data/gemfiles/3.1.gemfile +11 -12
  13. data/gemfiles/3.1.gemfile.lock +14 -10
  14. data/gemfiles/3.1_1.9.2.gemfile +12 -11
  15. data/gemfiles/3.1_1.9.2.gemfile.lock +14 -3
  16. data/gemfiles/3.2.gemfile +11 -12
  17. data/gemfiles/3.2.gemfile.lock +15 -10
  18. data/gemfiles/3.2_1.9.2.gemfile +12 -11
  19. data/gemfiles/3.2_1.9.2.gemfile.lock +14 -2
  20. data/gemfiles/4.0.0.gemfile +10 -12
  21. data/gemfiles/4.0.0.gemfile.lock +13 -10
  22. data/gemfiles/4.0.1.gemfile +10 -12
  23. data/gemfiles/4.0.1.gemfile.lock +13 -10
  24. data/gemfiles/4.1.gemfile +13 -15
  25. data/gemfiles/4.1.gemfile.lock +45 -50
  26. data/gemfiles/4.2.gemfile +36 -0
  27. data/gemfiles/4.2.gemfile.lock +245 -0
  28. data/lib/shoulda/matchers.rb +3 -1
  29. data/lib/shoulda/matchers/action_controller.rb +1 -1
  30. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +1 -1
  31. data/lib/shoulda/matchers/action_controller/route_params.rb +9 -4
  32. data/lib/shoulda/matchers/action_controller/{set_the_flash_matcher.rb → set_flash_matcher.rb} +34 -26
  33. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +125 -69
  34. data/lib/shoulda/matchers/active_model.rb +1 -2
  35. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +18 -5
  36. data/lib/shoulda/matchers/active_model/exception_message_finder.rb +2 -2
  37. data/lib/shoulda/matchers/active_model/helpers.rb +4 -4
  38. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +10 -3
  39. data/lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb +1 -1
  40. data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +3 -1
  41. data/lib/shoulda/matchers/active_model/{ensure_length_of_matcher.rb → validate_length_of_matcher.rb} +30 -20
  42. data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +21 -0
  43. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +2 -2
  44. data/lib/shoulda/matchers/active_record.rb +2 -0
  45. data/lib/shoulda/matchers/active_record/association_matcher.rb +96 -2
  46. data/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +1 -1
  47. data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +3 -3
  48. data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +22 -2
  49. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +30 -4
  50. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +19 -3
  51. data/lib/shoulda/matchers/active_record/uniqueness.rb +14 -0
  52. data/lib/shoulda/matchers/active_record/uniqueness/model.rb +45 -0
  53. data/lib/shoulda/matchers/active_record/uniqueness/namespace.rb +36 -0
  54. data/lib/shoulda/matchers/active_record/uniqueness/test_model_creator.rb +50 -0
  55. data/lib/shoulda/matchers/active_record/uniqueness/test_models.rb +24 -0
  56. data/lib/shoulda/matchers/{active_model → active_record}/validate_uniqueness_of_matcher.rb +76 -16
  57. data/lib/shoulda/matchers/independent/delegate_method_matcher.rb +117 -51
  58. data/lib/shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error.rb +1 -1
  59. data/lib/shoulda/matchers/matcher_context.rb +35 -0
  60. data/lib/shoulda/matchers/rails_shim.rb +23 -0
  61. data/lib/shoulda/matchers/util.rb +28 -0
  62. data/lib/shoulda/matchers/version.rb +1 -1
  63. data/script/SUPPORTED_VERSIONS +1 -1
  64. data/spec/acceptance/active_model_integration_spec.rb +20 -0
  65. data/spec/acceptance/independent_matchers_spec.rb +64 -0
  66. data/spec/acceptance/rails_integration_spec.rb +142 -0
  67. data/spec/acceptance_spec_helper.rb +23 -0
  68. data/spec/support/acceptance/helpers.rb +29 -0
  69. data/spec/support/acceptance/helpers/active_model_helpers.rb +11 -0
  70. data/spec/support/acceptance/helpers/array_helpers.rb +13 -0
  71. data/spec/support/acceptance/helpers/base_helpers.rb +14 -0
  72. data/spec/support/acceptance/helpers/command_helpers.rb +51 -0
  73. data/spec/support/acceptance/helpers/file_helpers.rb +19 -0
  74. data/spec/support/acceptance/helpers/gem_helpers.rb +31 -0
  75. data/spec/support/acceptance/helpers/minitest_helpers.rb +19 -0
  76. data/spec/support/acceptance/helpers/pluralization_helpers.rb +13 -0
  77. data/spec/support/acceptance/helpers/rails_version_helpers.rb +11 -0
  78. data/spec/support/acceptance/helpers/rspec_helpers.rb +26 -0
  79. data/spec/support/acceptance/helpers/ruby_version_helpers.rb +9 -0
  80. data/spec/support/acceptance/helpers/step_helpers.rb +117 -0
  81. data/spec/support/acceptance/matchers/have_output.rb +31 -0
  82. data/spec/support/acceptance/matchers/indicate_number_of_tests_was_run_matcher.rb +55 -0
  83. data/spec/support/acceptance/matchers/indicate_that_tests_were_run_matcher.rb +103 -0
  84. data/spec/support/tests/bundle.rb +94 -0
  85. data/spec/support/tests/command_runner.rb +214 -0
  86. data/spec/support/tests/filesystem.rb +77 -0
  87. data/spec/support/tests/version.rb +45 -0
  88. data/spec/support/unit/capture.rb +34 -0
  89. data/spec/support/unit/helpers/active_model_helpers.rb +25 -0
  90. data/spec/support/unit/helpers/active_model_versions.rb +20 -0
  91. data/spec/support/unit/helpers/active_resource_builder.rb +27 -0
  92. data/spec/support/unit/helpers/allow_value_matcher_helpers.rb +15 -0
  93. data/spec/support/unit/helpers/class_builder.rb +72 -0
  94. data/spec/support/unit/helpers/confirmation_matcher_helpers.rb +17 -0
  95. data/spec/support/unit/helpers/controller_builder.rb +91 -0
  96. data/spec/support/unit/helpers/i18n_faker.rb +15 -0
  97. data/spec/support/unit/helpers/mailer_builder.rb +12 -0
  98. data/spec/support/unit/helpers/model_builder.rb +102 -0
  99. data/spec/support/unit/helpers/rails_versions.rb +28 -0
  100. data/spec/support/unit/i18n.rb +7 -0
  101. data/spec/support/unit/matchers/deprecate.rb +60 -0
  102. data/spec/support/unit/matchers/fail_with_message_including_matcher.rb +50 -0
  103. data/spec/support/unit/matchers/fail_with_message_matcher.rb +50 -0
  104. data/spec/support/unit/matchers/print_warning_including.rb +59 -0
  105. data/spec/support/unit/rails_application.rb +110 -0
  106. data/spec/support/unit/record_builder_with_i18n_validation_message.rb +69 -0
  107. data/spec/support/unit/record_validating_confirmation_builder.rb +56 -0
  108. data/spec/support/unit/record_with_different_error_attribute_builder.rb +92 -0
  109. data/spec/support/{shared_examples → unit/shared_examples}/numerical_submatcher.rb +0 -2
  110. data/spec/support/{shared_examples → unit/shared_examples}/numerical_type_submatcher.rb +0 -2
  111. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/callback_matcher_spec.rb +2 -2
  112. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/filter_param_matcher_spec.rb +2 -2
  113. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/redirect_to_matcher_spec.rb +3 -3
  114. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/render_template_matcher_spec.rb +2 -4
  115. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/render_with_layout_matcher_spec.rb +9 -6
  116. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/rescue_from_matcher_spec.rb +2 -2
  117. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/respond_with_matcher_spec.rb +2 -2
  118. data/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +126 -0
  119. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/route_params_spec.rb +2 -2
  120. data/spec/unit/shoulda/matchers/action_controller/set_flash_matcher_spec.rb +167 -0
  121. data/spec/unit/shoulda/matchers/action_controller/set_session_matcher_spec.rb +294 -0
  122. data/spec/{shoulda → unit/shoulda}/matchers/action_controller/strong_parameters_matcher_spec.rb +19 -11
  123. data/spec/{shoulda → unit/shoulda}/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +2 -2
  124. data/spec/{shoulda → unit/shoulda}/matchers/active_model/allow_value_matcher_spec.rb +49 -21
  125. data/spec/{shoulda → unit/shoulda}/matchers/active_model/disallow_value_matcher_spec.rb +8 -4
  126. data/spec/{shoulda → unit/shoulda}/matchers/active_model/exception_message_finder_spec.rb +4 -4
  127. data/spec/{shoulda → unit/shoulda}/matchers/active_model/have_secure_password_matcher_spec.rb +2 -2
  128. data/spec/{shoulda → unit/shoulda}/matchers/active_model/helpers_spec.rb +7 -3
  129. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/comparison_matcher_spec.rb +1 -1
  130. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/even_number_matcher_spec.rb +1 -1
  131. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/odd_number_matcher_spec.rb +1 -1
  132. data/spec/{shoulda → unit/shoulda}/matchers/active_model/numericality_matchers/only_integer_matcher_spec.rb +1 -1
  133. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_absence_of_matcher_spec.rb +3 -3
  134. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_acceptance_of_matcher_spec.rb +2 -2
  135. data/spec/unit/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +63 -0
  136. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_exclusion_of_matcher_spec.rb +5 -4
  137. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_inclusion_of_matcher_spec.rb +7 -14
  138. data/spec/{shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb → unit/shoulda/matchers/active_model/validate_length_of_matcher_spec.rb} +43 -23
  139. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_numericality_of_matcher_spec.rb +3 -4
  140. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_presence_of_matcher_spec.rb +3 -3
  141. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +127 -2
  142. data/spec/{shoulda → unit/shoulda}/matchers/active_model/validation_message_finder_spec.rb +8 -6
  143. data/spec/{shoulda → unit/shoulda}/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +2 -2
  144. data/spec/{shoulda → unit/shoulda}/matchers/active_record/association_matcher_spec.rb +217 -26
  145. data/spec/{shoulda → unit/shoulda}/matchers/active_record/association_matchers/model_reflection_spec.rb +2 -2
  146. data/spec/{shoulda → unit/shoulda}/matchers/active_record/define_enum_for_matcher_spec.rb +2 -2
  147. data/spec/{shoulda → unit/shoulda}/matchers/active_record/have_db_column_matcher_spec.rb +2 -2
  148. data/spec/{shoulda → unit/shoulda}/matchers/active_record/have_db_index_matcher_spec.rb +8 -5
  149. data/spec/{shoulda → unit/shoulda}/matchers/active_record/have_readonly_attributes_matcher_spec.rb +2 -2
  150. data/spec/{shoulda → unit/shoulda}/matchers/active_record/serialize_matcher_spec.rb +3 -3
  151. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/double_collection_spec.rb +29 -7
  152. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/double_implementation_registry_spec.rb +1 -1
  153. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/double_spec.rb +20 -10
  154. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/object_double_spec.rb +1 -1
  155. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/proxy_implementation_spec.rb +13 -6
  156. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak/stub_implementation_spec.rb +2 -2
  157. data/spec/unit/shoulda/matchers/doublespeak/world_spec.rb +77 -0
  158. data/spec/{shoulda → unit/shoulda}/matchers/doublespeak_spec.rb +11 -3
  159. data/spec/{shoulda → unit/shoulda}/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb +1 -1
  160. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb +517 -0
  161. data/spec/unit_spec_helper.rb +66 -0
  162. data/spec/warnings_spy/partitioner.rb +10 -3
  163. data/spec/warnings_spy/reader.rb +9 -20
  164. data/spec/warnings_spy/reporter.rb +2 -1
  165. metadata +212 -149
  166. data/features/activemodel_integration.feature +0 -15
  167. data/features/rails_integration.feature +0 -160
  168. data/features/step_definitions/activemodel_steps.rb +0 -21
  169. data/features/step_definitions/rails_steps.rb +0 -227
  170. data/features/support/env.rb +0 -6
  171. data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +0 -70
  172. data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +0 -113
  173. data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +0 -153
  174. data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +0 -47
  175. data/spec/shoulda/matchers/doublespeak/world_spec.rb +0 -70
  176. data/spec/shoulda/matchers/independent/delegate_method_matcher_spec.rb +0 -309
  177. data/spec/spec_helper.rb +0 -42
  178. data/spec/support/active_model_versions.rb +0 -13
  179. data/spec/support/active_resource_builder.rb +0 -29
  180. data/spec/support/activemodel_helpers.rb +0 -23
  181. data/spec/support/capture_helpers.rb +0 -19
  182. data/spec/support/class_builder.rb +0 -46
  183. data/spec/support/controller_builder.rb +0 -102
  184. data/spec/support/fail_with_message_including_matcher.rb +0 -44
  185. data/spec/support/fail_with_message_matcher.rb +0 -44
  186. data/spec/support/i18n_faker.rb +0 -10
  187. data/spec/support/mailer_builder.rb +0 -10
  188. data/spec/support/model_builder.rb +0 -81
  189. data/spec/support/rails_versions.rb +0 -26
  190. data/spec/support/test_application.rb +0 -120
@@ -0,0 +1,517 @@
1
+ require 'unit_spec_helper'
2
+
3
+ describe Shoulda::Matchers::Independent::DelegateMethodMatcher do
4
+ describe '#description' do
5
+ context 'when the subject is an instance' do
6
+ subject { Object.new }
7
+
8
+ context 'without any qualifiers' do
9
+ it 'states that it should delegate method to the right object' do
10
+ matcher = delegate_method(:method_name).to(:delegate)
11
+ message = 'delegate #method_name to #delegate object'
12
+
13
+ expect(matcher.description).to eq message
14
+ end
15
+ end
16
+
17
+ context 'qualified with #as' do
18
+ it 'states that it should delegate method to the right object and method' do
19
+ matcher = delegate_method(:method_name).to(:delegate).as(:alternate)
20
+ message = 'delegate #method_name to #delegate object as #alternate'
21
+
22
+ expect(matcher.description).to eq message
23
+ end
24
+ end
25
+
26
+ context 'qualified with #with_arguments' do
27
+ it 'states that it should delegate method to the right object with right argument' do
28
+ matcher = delegate_method(:method_name).to(:delegate).
29
+ with_arguments(:foo, bar: [1, 2])
30
+ message = 'delegate #method_name to #delegate object passing arguments [:foo, {:bar=>[1, 2]}]'
31
+
32
+ expect(matcher.description).to eq message
33
+ end
34
+ end
35
+
36
+ context 'qualified with #with_prefix' do
37
+ context 'without arguments' do
38
+ before do
39
+ define_model('Country') do
40
+ def hello; 'hello' end
41
+ end
42
+ end
43
+
44
+ context "when the subject's delegating method also has a prefix" do
45
+ it 'produces the correct description' do
46
+ define_class('Person') do
47
+ delegate :hello, to: :country, prefix: true
48
+
49
+ def country
50
+ Country.new
51
+ end
52
+ end
53
+
54
+ matcher = delegate_method(:hello).to(:country).with_prefix
55
+ expect(matcher.description).
56
+ to eq('delegate #country_hello to #country object as #hello')
57
+ end
58
+ end
59
+ end
60
+
61
+ context 'as true' do
62
+ before do
63
+ define_model('Country') do
64
+ def hello; 'hello' end
65
+ end
66
+ end
67
+
68
+ context "when the subject's delegating method also has a prefix" do
69
+ it 'produces the correct description' do
70
+ define_class('Person') do
71
+ delegate :hello, to: :country, prefix: true
72
+
73
+ def country
74
+ Country.new
75
+ end
76
+ end
77
+
78
+ matcher = delegate_method(:hello).to(:country).with_prefix(true)
79
+ expect(matcher.description).
80
+ to eq('delegate #country_hello to #country object as #hello')
81
+ end
82
+ end
83
+ end
84
+
85
+ context 'as a symbol/string' do
86
+ it 'should delegate as (prefix_supplied)_(method_on_target)' do
87
+ define_model('Country') do
88
+ def hello; 'hello' end
89
+ end
90
+
91
+ define_class('Person') do
92
+ delegate :hello, to: :country, prefix: 'county'
93
+
94
+ def country
95
+ Country.new
96
+ end
97
+ end
98
+
99
+ matcher = delegate_method(:hello).to(:country).with_prefix('county')
100
+ expect(matcher.description).
101
+ to eq('delegate #county_hello to #country object as #hello')
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ context 'when the subject is a class' do
108
+ subject { Object }
109
+
110
+ context 'without any qualifiers' do
111
+ it 'states that it should delegate method to the right object' do
112
+ matcher = delegate_method(:method_name).to(:delegate)
113
+
114
+ expect(matcher.description).
115
+ to eq 'delegate .method_name to .delegate object'
116
+ end
117
+ end
118
+
119
+ context 'qualified with #as' do
120
+ it 'states that it should delegate method to the right object and method' do
121
+ matcher = delegate_method(:method_name).to(:delegate).as(:alternate)
122
+ message = 'delegate .method_name to .delegate object as .alternate'
123
+
124
+ expect(matcher.description).to eq message
125
+ end
126
+ end
127
+
128
+ context 'qualified with #with_arguments' do
129
+ it 'states that it should delegate method to the right object with right argument' do
130
+ matcher = delegate_method(:method_name).to(:delegate).
131
+ with_arguments(:foo, bar: [1, 2])
132
+ message = 'delegate .method_name to .delegate object passing arguments [:foo, {:bar=>[1, 2]}]'
133
+
134
+ expect(matcher.description).to eq message
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+ it 'raises an error if the delegate object was never specified before matching' do
141
+ expect {
142
+ expect(Object.new).to delegate_method(:name)
143
+ }.to raise_error described_class::DelegateObjectNotSpecified
144
+ end
145
+
146
+ context 'stubbing a delegating method on an instance' do
147
+ it 'only happens temporarily and is removed after the match' do
148
+ define_class('Company') do
149
+ def name
150
+ 'Acme Company'
151
+ end
152
+ end
153
+
154
+ define_class('Person') do
155
+ def company_name
156
+ company.name
157
+ end
158
+
159
+ def company
160
+ Company.new
161
+ end
162
+ end
163
+
164
+ person = Person.new
165
+ matcher = delegate_method(:company_name).to(:company).as(:name)
166
+ matcher.matches?(person)
167
+
168
+ expect(person.company.name).to eq 'Acme Company'
169
+ end
170
+ end
171
+
172
+ context 'when the subject does not delegate anything' do
173
+ before do
174
+ define_class('PostOffice')
175
+ end
176
+
177
+ context 'when the subject is an instance' do
178
+ it 'rejects with the correct failure message' do
179
+ post_office = PostOffice.new
180
+ message = [
181
+ 'Expected PostOffice to delegate #deliver_mail to #mailman object',
182
+ 'Method calls sent to PostOffice#mailman: (none)'
183
+ ].join("\n")
184
+
185
+ expect {
186
+ expect(post_office).to delegate_method(:deliver_mail).to(:mailman)
187
+ }.to fail_with_message(message)
188
+ end
189
+ end
190
+
191
+ context 'when the subject is a class' do
192
+ it 'uses the proper syntax for class methods in errors' do
193
+ message = [
194
+ 'Expected PostOffice to delegate .deliver_mail to .mailman object',
195
+ 'Method calls sent to PostOffice.mailman: (none)'
196
+ ].join("\n")
197
+
198
+ expect {
199
+ expect(PostOffice).to delegate_method(:deliver_mail).to(:mailman)
200
+ }.to fail_with_message(message)
201
+ end
202
+ end
203
+ end
204
+
205
+ context 'when the subject delegates correctly' do
206
+ before do
207
+ define_class('Mailman')
208
+
209
+ define_class('PostOffice') do
210
+ def deliver_mail
211
+ mailman.deliver_mail
212
+ end
213
+
214
+ def mailman
215
+ Mailman.new
216
+ end
217
+ end
218
+ end
219
+
220
+ it 'accepts' do
221
+ post_office = PostOffice.new
222
+ expect(post_office).to delegate_method(:deliver_mail).to(:mailman)
223
+ end
224
+
225
+ context 'negating the matcher' do
226
+ it 'rejects with the correct failure message' do
227
+ post_office = PostOffice.new
228
+ message = 'Expected PostOffice not to delegate #deliver_mail to #mailman object, but it did'
229
+
230
+ expect {
231
+ expect(post_office).not_to delegate_method(:deliver_mail).to(:mailman)
232
+ }.to fail_with_message(message)
233
+ end
234
+ end
235
+ end
236
+
237
+ context 'when the delegating method is private' do
238
+ before do
239
+ define_class('Mailman')
240
+
241
+ define_class('PostOffice') do
242
+ def deliver_mail
243
+ mailman.deliver_mail
244
+ end
245
+
246
+ def mailman
247
+ Mailman.new
248
+ end
249
+
250
+ private :mailman
251
+ end
252
+ end
253
+
254
+ it 'accepts' do
255
+ post_office = PostOffice.new
256
+ expect(post_office).to delegate_method(:deliver_mail).to(:mailman)
257
+ end
258
+ end
259
+
260
+ context 'qualified with #with_arguments' do
261
+ before do
262
+ define_class('Mailman')
263
+
264
+ define_class('PostOffice') do
265
+ def deliver_mail(*args)
266
+ mailman.deliver_mail('221B Baker St.', hastily: true)
267
+ end
268
+
269
+ def mailman
270
+ Mailman.new
271
+ end
272
+ end
273
+ end
274
+
275
+ context 'qualified with #with_arguments' do
276
+ context 'when the subject delegates with matching arguments' do
277
+ it 'accepts' do
278
+ post_office = PostOffice.new
279
+ expect(post_office).to delegate_method(:deliver_mail).
280
+ to(:mailman).with_arguments('221B Baker St.', hastily: true)
281
+ end
282
+
283
+ context 'negating the matcher' do
284
+ it 'rejects with the correct failure message' do
285
+ post_office = PostOffice.new
286
+ message = 'Expected PostOffice not to delegate #deliver_mail to #mailman object passing arguments ["221B Baker St.", {:hastily=>true}], but it did'
287
+
288
+ expect {
289
+ expect(post_office).
290
+ not_to delegate_method(:deliver_mail).
291
+ to(:mailman).
292
+ with_arguments('221B Baker St.', hastily: true)
293
+ }.to fail_with_message(message)
294
+ end
295
+ end
296
+ end
297
+
298
+ context 'when not given the correct arguments' do
299
+ it 'rejects with the correct failure message' do
300
+ post_office = PostOffice.new
301
+ message = [
302
+ 'Expected PostOffice to delegate #deliver_mail to #mailman object passing arguments ["123 Nowhere Ln."]',
303
+ 'Method calls sent to PostOffice#mailman:',
304
+ '1) deliver_mail("221B Baker St.", {:hastily=>true})'
305
+ ].join("\n")
306
+
307
+ expect {
308
+ expect(post_office).to delegate_method(:deliver_mail).
309
+ to(:mailman).with_arguments('123 Nowhere Ln.')
310
+ }.to fail_with_message(message)
311
+ end
312
+ end
313
+ end
314
+ end
315
+
316
+ context 'qualified with #as' do
317
+ before do
318
+ define_class(:mailman)
319
+
320
+ define_class(:post_office) do
321
+ def deliver_mail
322
+ mailman.deliver_mail_and_avoid_dogs
323
+ end
324
+
325
+ def mailman
326
+ Mailman.new
327
+ end
328
+ end
329
+ end
330
+
331
+ context "when the given method is the same as the subject's delegating method" do
332
+ it 'accepts' do
333
+ post_office = PostOffice.new
334
+ expect(post_office).to delegate_method(:deliver_mail).
335
+ to(:mailman).as(:deliver_mail_and_avoid_dogs)
336
+ end
337
+
338
+ context 'negating the assertion' do
339
+ it 'rejects with the correct failure message' do
340
+ post_office = PostOffice.new
341
+ message = 'Expected PostOffice not to delegate #deliver_mail to #mailman object as #deliver_mail_and_avoid_dogs, but it did'
342
+
343
+ expect {
344
+ expect(post_office).
345
+ not_to delegate_method(:deliver_mail).
346
+ to(:mailman).
347
+ as(:deliver_mail_and_avoid_dogs)
348
+ }.to fail_with_message(message)
349
+ end
350
+ end
351
+ end
352
+
353
+ context "when the given method is not the same as the subject's delegating method" do
354
+ it 'rejects with the correct failure message' do
355
+ post_office = PostOffice.new
356
+ message = [
357
+ 'Expected PostOffice to delegate #deliver_mail to #mailman object as #watch_tv',
358
+ 'Method calls sent to PostOffice#mailman:',
359
+ '1) deliver_mail_and_avoid_dogs()'
360
+ ].join("\n")
361
+
362
+ expect {
363
+ expect(post_office).to delegate_method(:deliver_mail).
364
+ to(:mailman).as(:watch_tv)
365
+ }.to fail_with_message(message)
366
+ end
367
+ end
368
+ end
369
+
370
+ context 'qualified with #with_prefix' do
371
+ context 'without arguments' do
372
+ before do
373
+ define_model('Country') do
374
+ def hello; 'hello' end
375
+ end
376
+ end
377
+
378
+ context "when the subject's delegating method also has a prefix" do
379
+ it 'accepts' do
380
+ define_class('Person') do
381
+ delegate :hello, to: :country, prefix: true
382
+
383
+ def country
384
+ Country.new
385
+ end
386
+ end
387
+
388
+ person = Person.new
389
+ expect(person).to delegate_method(:hello). to(:country).with_prefix
390
+ end
391
+ end
392
+
393
+ context "when the subject's delegating method does not have a prefix" do
394
+ it 'rejects with the correct failure message' do
395
+ define_class('Person') do
396
+ delegate :hello, to: :country
397
+
398
+ def country
399
+ Country.new
400
+ end
401
+ end
402
+
403
+ message = [
404
+ 'Expected Person to delegate #country_hello to #country object as #hello',
405
+ 'Method calls sent to Person#country: (none)'
406
+ ].join("\n")
407
+
408
+ person = Person.new
409
+
410
+ expect {
411
+ expect(person).to delegate_method(:hello). to(:country).with_prefix
412
+ }.to fail_with_message(message)
413
+ end
414
+ end
415
+ end
416
+
417
+ context 'as true' do
418
+ before do
419
+ define_model('Country') do
420
+ def hello; 'hello' end
421
+ end
422
+ end
423
+
424
+ context "when the subject's delegating method also has a prefix" do
425
+ it 'accepts' do
426
+ define_class('Person') do
427
+ delegate :hello, to: :country, prefix: true
428
+
429
+ def country
430
+ Country.new
431
+ end
432
+ end
433
+
434
+ person = Person.new
435
+ expect(person).
436
+ to delegate_method(:hello).
437
+ to(:country).with_prefix(true)
438
+ end
439
+ end
440
+
441
+ context "when the subject's delegating method does not have a prefix" do
442
+ it 'rejects with the correct failure message' do
443
+ define_class('Person') do
444
+ delegate :hello, to: :country
445
+
446
+ def country
447
+ Country.new
448
+ end
449
+ end
450
+
451
+ message = [
452
+ 'Expected Person to delegate #country_hello to #country object as #hello',
453
+ 'Method calls sent to Person#country: (none)'
454
+ ].join("\n")
455
+
456
+ person = Person.new
457
+
458
+ expect {
459
+ expect(person).
460
+ to delegate_method(:hello).
461
+ to(:country).with_prefix(true)
462
+ }.to fail_with_message(message)
463
+ end
464
+ end
465
+ end
466
+
467
+ context 'as a symbol/string' do
468
+ before do
469
+ define_model('Country') do
470
+ def hello; 'hello' end
471
+ end
472
+ end
473
+
474
+ context "when the subject's delegating method has the same prefix" do
475
+ it 'accepts' do
476
+ define_class('Person') do
477
+ delegate :hello, to: :country, prefix: 'county'
478
+
479
+ def country
480
+ Country.new
481
+ end
482
+ end
483
+
484
+ person = Person.new
485
+ expect(person).
486
+ to delegate_method(:hello).
487
+ to(:country).with_prefix('county')
488
+ end
489
+ end
490
+
491
+ context "when the subject's delegating method has a different prefix" do
492
+ it 'rejects with the correct failure message' do
493
+ define_class('Person') do
494
+ delegate :hello, to: :country, prefix: 'something_else'
495
+
496
+ def country
497
+ Country.new
498
+ end
499
+ end
500
+
501
+ message = [
502
+ 'Expected Person to delegate #county_hello to #country object as #hello',
503
+ 'Method calls sent to Person#country: (none)'
504
+ ].join("\n")
505
+
506
+ person = Person.new
507
+
508
+ expect {
509
+ expect(person).
510
+ to delegate_method(:hello).
511
+ to(:country).with_prefix('county')
512
+ }.to fail_with_message(message)
513
+ end
514
+ end
515
+ end
516
+ end
517
+ end