shoulda-matchers 4.0.0.rc1 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +72 -40
  4. data/lib/shoulda/matchers/action_controller.rb +2 -0
  5. data/lib/shoulda/matchers/active_model.rb +2 -3
  6. data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +1 -0
  7. data/lib/shoulda/matchers/active_record.rb +2 -0
  8. data/lib/shoulda/matchers/active_record/association_matcher.rb +34 -0
  9. data/lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb +27 -4
  10. data/lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb +27 -4
  11. data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +1 -1
  12. data/lib/shoulda/matchers/independent.rb +2 -1
  13. data/lib/shoulda/matchers/rails_shim.rb +5 -9
  14. data/lib/shoulda/matchers/version.rb +1 -1
  15. data/lib/shoulda/matchers/warn.rb +1 -0
  16. data/shoulda-matchers.gemspec +11 -3
  17. metadata +13 -340
  18. data/.gitignore +0 -12
  19. data/.hound.yml +0 -3
  20. data/.hound/ruby.yml +0 -1062
  21. data/.python-version +0 -1
  22. data/.rubocop.yml +0 -15
  23. data/.travis.yml +0 -21
  24. data/.yardopts +0 -10
  25. data/Appraisals +0 -105
  26. data/CONTRIBUTING.md +0 -172
  27. data/Gemfile +0 -15
  28. data/Gemfile.lock +0 -59
  29. data/MAINTAINING.md +0 -250
  30. data/NEWS.md +0 -1235
  31. data/Rakefile +0 -46
  32. data/bin/setup +0 -190
  33. data/custom_plan.rb +0 -104
  34. data/doc_config/gh-pages/index.html.erb +0 -9
  35. data/doc_config/yard/setup.rb +0 -22
  36. data/doc_config/yard/templates/default/fulldoc/html/css/bootstrap.css +0 -5967
  37. data/doc_config/yard/templates/default/fulldoc/html/css/full_list.css +0 -12
  38. data/doc_config/yard/templates/default/fulldoc/html/css/global.css +0 -66
  39. data/doc_config/yard/templates/default/fulldoc/html/css/solarized.css +0 -69
  40. data/doc_config/yard/templates/default/fulldoc/html/css/style.css +0 -312
  41. data/doc_config/yard/templates/default/fulldoc/html/full_list.erb +0 -26
  42. data/doc_config/yard/templates/default/fulldoc/html/full_list_class.erb +0 -1
  43. data/doc_config/yard/templates/default/fulldoc/html/full_list_method.erb +0 -8
  44. data/doc_config/yard/templates/default/fulldoc/html/js/app.js +0 -281
  45. data/doc_config/yard/templates/default/fulldoc/html/js/full_list.js +0 -1
  46. data/doc_config/yard/templates/default/fulldoc/html/js/jquery.stickyheaders.js +0 -289
  47. data/doc_config/yard/templates/default/fulldoc/html/js/underscore.min.js +0 -6
  48. data/doc_config/yard/templates/default/fulldoc/html/setup.rb +0 -35
  49. data/doc_config/yard/templates/default/layout/html/breadcrumb.erb +0 -14
  50. data/doc_config/yard/templates/default/layout/html/fonts.erb +0 -1
  51. data/doc_config/yard/templates/default/layout/html/footer.erb +0 -6
  52. data/doc_config/yard/templates/default/layout/html/layout.erb +0 -23
  53. data/doc_config/yard/templates/default/layout/html/search.erb +0 -13
  54. data/doc_config/yard/templates/default/layout/html/setup.rb +0 -40
  55. data/doc_config/yard/templates/default/method_details/html/source.erb +0 -10
  56. data/doc_config/yard/templates/default/module/html/box_info.erb +0 -31
  57. data/gemfiles/4.2.gemfile +0 -39
  58. data/gemfiles/4.2.gemfile.lock +0 -246
  59. data/gemfiles/5.0.gemfile +0 -37
  60. data/gemfiles/5.0.gemfile.lock +0 -238
  61. data/gemfiles/5.1.gemfile +0 -38
  62. data/gemfiles/5.1.gemfile.lock +0 -254
  63. data/gemfiles/5.2.gemfile +0 -40
  64. data/gemfiles/5.2.gemfile.lock +0 -273
  65. data/script/install_gems_in_all_appraisals +0 -16
  66. data/script/run_all_tests +0 -16
  67. data/script/supported_ruby_versions +0 -7
  68. data/script/update_gem_in_all_appraisals +0 -17
  69. data/script/update_gems_in_all_appraisals +0 -16
  70. data/spec/acceptance/active_model_integration_spec.rb +0 -23
  71. data/spec/acceptance/independent_matchers_spec.rb +0 -125
  72. data/spec/acceptance/multiple_libraries_integration_spec.rb +0 -55
  73. data/spec/acceptance/rails_integration_spec.rb +0 -156
  74. data/spec/acceptance_spec_helper.rb +0 -23
  75. data/spec/doublespeak_spec_helper.rb +0 -2
  76. data/spec/report_warnings.rb +0 -7
  77. data/spec/spec_helper.rb +0 -20
  78. data/spec/support/acceptance/adds_shoulda_matchers_to_project.rb +0 -133
  79. data/spec/support/acceptance/helpers.rb +0 -33
  80. data/spec/support/acceptance/helpers/active_model_helpers.rb +0 -11
  81. data/spec/support/acceptance/helpers/array_helpers.rb +0 -13
  82. data/spec/support/acceptance/helpers/base_helpers.rb +0 -19
  83. data/spec/support/acceptance/helpers/command_helpers.rb +0 -68
  84. data/spec/support/acceptance/helpers/file_helpers.rb +0 -19
  85. data/spec/support/acceptance/helpers/gem_helpers.rb +0 -31
  86. data/spec/support/acceptance/helpers/minitest_helpers.rb +0 -11
  87. data/spec/support/acceptance/helpers/n_unit_helpers.rb +0 -25
  88. data/spec/support/acceptance/helpers/pluralization_helpers.rb +0 -13
  89. data/spec/support/acceptance/helpers/rails_migration_helpers.rb +0 -21
  90. data/spec/support/acceptance/helpers/rails_version_helpers.rb +0 -11
  91. data/spec/support/acceptance/helpers/rspec_helpers.rb +0 -24
  92. data/spec/support/acceptance/helpers/ruby_version_helpers.rb +0 -9
  93. data/spec/support/acceptance/helpers/step_helpers.rb +0 -127
  94. data/spec/support/acceptance/matchers/have_output.rb +0 -31
  95. data/spec/support/acceptance/matchers/indicate_number_of_tests_was_run_matcher.rb +0 -55
  96. data/spec/support/acceptance/matchers/indicate_that_tests_were_run_matcher.rb +0 -103
  97. data/spec/support/tests/bundle.rb +0 -94
  98. data/spec/support/tests/command_runner.rb +0 -230
  99. data/spec/support/tests/current_bundle.rb +0 -55
  100. data/spec/support/tests/database.rb +0 -28
  101. data/spec/support/tests/database_adapters/postgresql.rb +0 -25
  102. data/spec/support/tests/database_adapters/sqlite3.rb +0 -26
  103. data/spec/support/tests/database_configuration.rb +0 -33
  104. data/spec/support/tests/database_configuration_registry.rb +0 -28
  105. data/spec/support/tests/filesystem.rb +0 -100
  106. data/spec/support/tests/version.rb +0 -45
  107. data/spec/support/unit/active_record/create_table.rb +0 -54
  108. data/spec/support/unit/attribute.rb +0 -45
  109. data/spec/support/unit/capture.rb +0 -46
  110. data/spec/support/unit/change_value.rb +0 -111
  111. data/spec/support/unit/create_model_arguments/basic.rb +0 -135
  112. data/spec/support/unit/create_model_arguments/has_many.rb +0 -15
  113. data/spec/support/unit/create_model_arguments/uniqueness_matcher.rb +0 -74
  114. data/spec/support/unit/helpers/action_pack_versions.rb +0 -22
  115. data/spec/support/unit/helpers/active_model_helpers.rb +0 -27
  116. data/spec/support/unit/helpers/active_model_versions.rb +0 -32
  117. data/spec/support/unit/helpers/active_record_versions.rb +0 -44
  118. data/spec/support/unit/helpers/active_resource_builder.rb +0 -27
  119. data/spec/support/unit/helpers/allow_value_matcher_helpers.rb +0 -15
  120. data/spec/support/unit/helpers/class_builder.rb +0 -90
  121. data/spec/support/unit/helpers/column_type_helpers.rb +0 -26
  122. data/spec/support/unit/helpers/confirmation_matcher_helpers.rb +0 -17
  123. data/spec/support/unit/helpers/controller_builder.rb +0 -63
  124. data/spec/support/unit/helpers/database_helpers.rb +0 -20
  125. data/spec/support/unit/helpers/i18n_faker.rb +0 -15
  126. data/spec/support/unit/helpers/mailer_builder.rb +0 -12
  127. data/spec/support/unit/helpers/message_helpers.rb +0 -19
  128. data/spec/support/unit/helpers/model_builder.rb +0 -114
  129. data/spec/support/unit/helpers/rails_versions.rb +0 -42
  130. data/spec/support/unit/helpers/validation_matcher_scenario_helpers.rb +0 -44
  131. data/spec/support/unit/i18n.rb +0 -7
  132. data/spec/support/unit/load_environment.rb +0 -12
  133. data/spec/support/unit/matchers/deprecate.rb +0 -60
  134. data/spec/support/unit/matchers/fail_with_message_including_matcher.rb +0 -51
  135. data/spec/support/unit/matchers/fail_with_message_matcher.rb +0 -64
  136. data/spec/support/unit/matchers/print_warning_including.rb +0 -67
  137. data/spec/support/unit/model_creation_strategies/active_model.rb +0 -111
  138. data/spec/support/unit/model_creation_strategies/active_record.rb +0 -77
  139. data/spec/support/unit/model_creators.rb +0 -19
  140. data/spec/support/unit/model_creators/active_model.rb +0 -39
  141. data/spec/support/unit/model_creators/active_record.rb +0 -42
  142. data/spec/support/unit/model_creators/active_record/has_and_belongs_to_many.rb +0 -95
  143. data/spec/support/unit/model_creators/active_record/has_many.rb +0 -67
  144. data/spec/support/unit/model_creators/active_record/uniqueness_matcher.rb +0 -42
  145. data/spec/support/unit/model_creators/basic.rb +0 -102
  146. data/spec/support/unit/rails_application.rb +0 -151
  147. data/spec/support/unit/record_builder_with_i18n_validation_message.rb +0 -69
  148. data/spec/support/unit/record_validating_confirmation_builder.rb +0 -54
  149. data/spec/support/unit/record_with_different_error_attribute_builder.rb +0 -92
  150. data/spec/support/unit/shared_examples/ignoring_interference_by_writer.rb +0 -79
  151. data/spec/support/unit/shared_examples/numerical_submatcher.rb +0 -17
  152. data/spec/support/unit/shared_examples/set_session_or_flash.rb +0 -360
  153. data/spec/support/unit/validation_matcher_scenario.rb +0 -60
  154. data/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb +0 -82
  155. data/spec/unit/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +0 -28
  156. data/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb +0 -629
  157. data/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +0 -42
  158. data/spec/unit/shoulda/matchers/action_controller/render_template_matcher_spec.rb +0 -76
  159. data/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +0 -62
  160. data/spec/unit/shoulda/matchers/action_controller/rescue_from_matcher_spec.rb +0 -90
  161. data/spec/unit/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +0 -31
  162. data/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +0 -330
  163. data/spec/unit/shoulda/matchers/action_controller/route_params_spec.rb +0 -30
  164. data/spec/unit/shoulda/matchers/action_controller/set_flash_matcher_spec.rb +0 -67
  165. data/spec/unit/shoulda/matchers/action_controller/set_session_matcher_spec.rb +0 -17
  166. data/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb +0 -562
  167. data/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -117
  168. data/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb +0 -829
  169. data/spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +0 -86
  170. data/spec/unit/shoulda/matchers/active_model/have_secure_password_matcher_spec.rb +0 -20
  171. data/spec/unit/shoulda/matchers/active_model/helpers_spec.rb +0 -162
  172. data/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +0 -290
  173. data/spec/unit/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +0 -109
  174. data/spec/unit/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +0 -172
  175. data/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb +0 -264
  176. data/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +0 -1049
  177. data/spec/unit/shoulda/matchers/active_model/validate_length_of_matcher_spec.rb +0 -335
  178. data/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +0 -1865
  179. data/spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +0 -406
  180. data/spec/unit/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -107
  181. data/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +0 -1672
  182. data/spec/unit/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb +0 -251
  183. data/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb +0 -690
  184. data/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +0 -111
  185. data/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +0 -85
  186. data/spec/unit/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +0 -41
  187. data/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb +0 -169
  188. data/spec/unit/shoulda/matchers/active_record/serialize_matcher_spec.rb +0 -86
  189. data/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb +0 -1682
  190. data/spec/unit/shoulda/matchers/doublespeak/double_collection_spec.rb +0 -190
  191. data/spec/unit/shoulda/matchers/doublespeak/double_implementation_registry_spec.rb +0 -21
  192. data/spec/unit/shoulda/matchers/doublespeak/double_spec.rb +0 -271
  193. data/spec/unit/shoulda/matchers/doublespeak/object_double_spec.rb +0 -77
  194. data/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb +0 -72
  195. data/spec/unit/shoulda/matchers/doublespeak/stub_implementation_spec.rb +0 -101
  196. data/spec/unit/shoulda/matchers/doublespeak/world_spec.rb +0 -78
  197. data/spec/unit/shoulda/matchers/doublespeak_spec.rb +0 -27
  198. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb +0 -43
  199. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb +0 -650
  200. data/spec/unit/shoulda/matchers/routing/route_matcher_spec.rb +0 -406
  201. data/spec/unit/shoulda/matchers/util/word_wrap_spec.rb +0 -252
  202. data/spec/unit_spec_helper.rb +0 -54
  203. data/spec/warnings_spy.rb +0 -64
  204. data/spec/warnings_spy/filesystem.rb +0 -45
  205. data/spec/warnings_spy/partitioner.rb +0 -36
  206. data/spec/warnings_spy/reader.rb +0 -53
  207. data/spec/warnings_spy/reporter.rb +0 -88
  208. data/tasks/documentation.rb +0 -199
  209. data/zeus.json +0 -11
@@ -1,190 +0,0 @@
1
- require 'doublespeak_spec_helper'
2
-
3
- module Shoulda::Matchers::Doublespeak
4
- describe DoubleCollection do
5
- describe '#register_stub' do
6
- it 'calls DoubleImplementationRegistry.find correctly' do
7
- allow(DoubleImplementationRegistry).to receive(:find)
8
- double_collection = described_class.new(build_world, :klass)
9
-
10
- double_collection.register_stub(:a_method)
11
-
12
- expect(DoubleImplementationRegistry).to have_received(:find).with(:stub)
13
- end
14
-
15
- it 'calls Double.new correctly' do
16
- world = build_world
17
- allow(DoubleImplementationRegistry).
18
- to receive(:find).
19
- and_return(:implementation)
20
- allow(Double).to receive(:new)
21
- double_collection = described_class.new(world, :klass)
22
-
23
- double_collection.register_stub(:a_method)
24
-
25
- expect(Double).
26
- to have_received(:new).
27
- with(world, :klass, :a_method, :implementation)
28
- end
29
-
30
- context 'if a double has already been registered for the method' do
31
- it 'does not call Double.new again' do
32
- world = build_world
33
- allow(DoubleImplementationRegistry).
34
- to receive(:find).
35
- and_return(:implementation)
36
- allow(Double).to receive(:new)
37
- double_collection = described_class.new(world, :klass)
38
-
39
- double_collection.register_stub(:a_method)
40
- double_collection.register_stub(:a_method)
41
-
42
- expect(Double).to have_received(:new).once
43
- end
44
-
45
- it 'returns the same Double' do
46
- world = build_world
47
- allow(DoubleImplementationRegistry).
48
- to receive(:find).
49
- and_return(:implementation)
50
- allow(Double).to receive(:new)
51
- double_collection = described_class.new(world, :klass)
52
-
53
- double1 = double_collection.register_stub(:a_method)
54
- double2 = double_collection.register_stub(:a_method)
55
-
56
- expect(double1).to equal(double2)
57
- end
58
- end
59
- end
60
-
61
- describe '#register_proxy' do
62
- it 'calls DoubleImplementationRegistry.find correctly' do
63
- allow(DoubleImplementationRegistry).to receive(:find)
64
- double_collection = described_class.new(build_world, :klass)
65
-
66
- double_collection.register_proxy(:a_method)
67
-
68
- expect(DoubleImplementationRegistry).
69
- to have_received(:find).
70
- with(:proxy)
71
- end
72
-
73
- it 'calls Double.new correctly' do
74
- world = build_world
75
- allow(DoubleImplementationRegistry).
76
- to receive(:find).
77
- and_return(:implementation)
78
- allow(Double).to receive(:new)
79
- double_collection = described_class.new(world, :klass)
80
-
81
- double_collection.register_proxy(:a_method)
82
-
83
- expect(Double).
84
- to have_received(:new).
85
- with(world, :klass, :a_method, :implementation)
86
- end
87
-
88
- context 'if a double has already been registered for the method' do
89
- it 'does not call Double.new again' do
90
- world = build_world
91
- allow(DoubleImplementationRegistry).
92
- to receive(:find).
93
- and_return(:implementation)
94
- allow(Double).to receive(:new)
95
- double_collection = described_class.new(world, :klass)
96
-
97
- double_collection.register_proxy(:a_method)
98
- double_collection.register_proxy(:a_method)
99
-
100
- expect(Double).to have_received(:new).once
101
- end
102
-
103
- it 'returns the same Double' do
104
- world = build_world
105
- allow(DoubleImplementationRegistry).
106
- to receive(:find).
107
- and_return(:implementation)
108
- allow(Double).to receive(:new)
109
- double_collection = described_class.new(world, :klass)
110
-
111
- double1 = double_collection.register_proxy(:a_method)
112
- double2 = double_collection.register_proxy(:a_method)
113
-
114
- expect(double1).to equal(double2)
115
- end
116
- end
117
- end
118
-
119
- describe '#activate' do
120
- it 'replaces all registered methods with doubles' do
121
- klass = create_class(first_method: 1, second_method: 2)
122
- double_collection = described_class.new(build_world, klass)
123
- double_collection.register_stub(:first_method)
124
- double_collection.register_stub(:second_method)
125
-
126
- double_collection.activate
127
-
128
- instance = klass.new
129
- expect(instance.first_method).to eq nil
130
- expect(instance.second_method).to eq nil
131
- end
132
- end
133
-
134
- describe '#deactivate' do
135
- it 'restores the original methods that were doubled' do
136
- klass = create_class(first_method: 1, second_method: 2)
137
- double_collection = described_class.new(build_world, klass)
138
- double_collection.register_stub(:first_method)
139
- double_collection.register_stub(:second_method)
140
-
141
- double_collection.activate
142
- double_collection.deactivate
143
-
144
- instance = klass.new
145
- expect(instance.first_method).to eq 1
146
- expect(instance.second_method).to eq 2
147
- end
148
- end
149
-
150
- describe '#calls_to' do
151
- it 'returns all calls to the given method' do
152
- klass = create_class(a_method: nil)
153
- double_collection = described_class.new(build_world, klass)
154
- double_collection.register_stub(:a_method)
155
- double_collection.activate
156
-
157
- actual_calls = [
158
- { args: [:some, :args, :here] },
159
- { args: [:some, :args], block: -> { :whatever } }
160
- ]
161
- instance = klass.new
162
- instance.a_method(*actual_calls[0][:args])
163
- instance.a_method(*actual_calls[1][:args], &actual_calls[1][:block])
164
-
165
- calls = double_collection.calls_to(:a_method)
166
- expect(calls[0].args).to eq actual_calls[0][:args]
167
- expect(calls[1].args).to eq actual_calls[1][:args]
168
- expect(calls[1].block).to eq actual_calls[1][:block]
169
- end
170
-
171
- it 'returns an empty array if the method has never been doubled' do
172
- klass = create_class
173
- double_collection = described_class.new(build_world, klass)
174
- expect(double_collection.calls_to(:non_existent_method)).to eq []
175
- end
176
- end
177
-
178
- def create_class(methods = {})
179
- Class.new.tap do |klass|
180
- methods.each do |name, value|
181
- klass.__send__(:define_method, name) { |*args| value }
182
- end
183
- end
184
- end
185
-
186
- def build_world
187
- Shoulda::Matchers::Doublespeak::World.new
188
- end
189
- end
190
- end
@@ -1,21 +0,0 @@
1
- require 'doublespeak_spec_helper'
2
-
3
- module Shoulda::Matchers::Doublespeak
4
- describe DoubleImplementationRegistry do
5
- describe '.find' do
6
- it 'returns an instance of StubImplementation if given :stub' do
7
- expect(described_class.find(:stub)).to be_a(StubImplementation)
8
- end
9
-
10
- it 'returns ProxyImplementation if given :proxy' do
11
- expect(described_class.find(:proxy)).to be_a(ProxyImplementation)
12
- end
13
-
14
- it 'raises an ArgumentError if not given a registered implementation' do
15
- expect {
16
- expect(described_class.find(:something_else))
17
- }.to raise_error(ArgumentError)
18
- end
19
- end
20
- end
21
- end
@@ -1,271 +0,0 @@
1
- require 'doublespeak_spec_helper'
2
-
3
- module Shoulda::Matchers::Doublespeak
4
- describe Double do
5
- describe 'initializer' do
6
- context 'if doubles are currently activated on the world level' do
7
- it 'immediately activates the new Double' do
8
- world = build_world(doubles_activated?: true)
9
- klass = create_class(a_method_name: nil)
10
- implementation = build_implementation
11
-
12
- double = described_class.new(world, klass, :a_method_name, implementation)
13
-
14
- expect(double).to be_activated
15
- end
16
- end
17
- end
18
-
19
- describe '#to_return' do
20
- it 'tells its implementation to call the given block' do
21
- sent_block = -> { }
22
- actual_block = nil
23
- implementation = build_implementation
24
- implementation.singleton_class.__send__(:undef_method, :returns)
25
- implementation.singleton_class.__send__(:define_method, :returns) do |&block|
26
- actual_block = block
27
- end
28
- double = described_class.new(
29
- build_world,
30
- :klass,
31
- :a_method,
32
- implementation
33
- )
34
- double.to_return(&sent_block)
35
- expect(actual_block).to eq sent_block
36
- end
37
-
38
- it 'tells its implementation to return the given value' do
39
- implementation = build_implementation
40
- double = described_class.new(
41
- build_world,
42
- :klass,
43
- :a_method,
44
- implementation
45
- )
46
- double.to_return(:implementation)
47
-
48
- expect(implementation).to have_received(:returns).with(:implementation)
49
- end
50
-
51
- it 'prefers a block over a non-block' do
52
- sent_block = -> { }
53
- actual_block = nil
54
- implementation = build_implementation
55
- implementation.singleton_class.__send__(:undef_method, :returns)
56
- implementation.singleton_class.__send__(:define_method, :returns) do |&block|
57
- actual_block = block
58
- end
59
- double = described_class.new(
60
- build_world,
61
- :klass,
62
- :a_method,
63
- implementation
64
- )
65
- double.to_return(:value, &sent_block)
66
- expect(actual_block).to eq sent_block
67
- end
68
- end
69
-
70
- describe '#activate' do
71
- it 'replaces the method with an implementation' do
72
- implementation = build_implementation
73
- method_name = :a_method
74
- klass = create_class(method_name => :some_return_value)
75
- instance = klass.new
76
- double = described_class.new(
77
- build_world,
78
- klass,
79
- method_name,
80
- implementation
81
- )
82
- args = [:any, :args]
83
- block = -> {}
84
- call = MethodCall.new(
85
- double: double,
86
- object: instance,
87
- method_name: method_name,
88
- args: args,
89
- block: block,
90
- caller: :some_caller
91
- )
92
-
93
- double.activate
94
- instance.__send__(method_name, *args, &block)
95
-
96
- expect(implementation).
97
- to have_received(:call).
98
- with(call)
99
- end
100
- end
101
-
102
- describe '#deactivate' do
103
- it 'restores the original method after being doubled' do
104
- klass = create_class(a_method: 42)
105
- world = build_world(
106
- original_method_for: klass.instance_method(:a_method)
107
- )
108
- instance = klass.new
109
- double = described_class.new(
110
- world,
111
- klass,
112
- :a_method,
113
- build_implementation
114
- )
115
-
116
- double.activate
117
- double.deactivate
118
- expect(instance.a_method).to eq 42
119
- end
120
-
121
- it 'still restores the original method if #activate was called twice' do
122
- method_name = :a_method
123
- klass = create_class(method_name => 42)
124
- world = build_world(
125
- original_method_for: klass.instance_method(:a_method)
126
- )
127
- instance = klass.new
128
- double = described_class.new(
129
- world,
130
- klass,
131
- :a_method,
132
- build_implementation
133
- )
134
-
135
- double.activate
136
- double.activate
137
- double.deactivate
138
- expect(instance.a_method).to eq 42
139
- end
140
-
141
- it 'does nothing if the method has not been doubled' do
142
- klass = create_class(a_method: 42)
143
- instance = klass.new
144
- double = described_class.new(
145
- build_world,
146
- klass,
147
- :a_method,
148
- build_implementation
149
- )
150
-
151
- double.deactivate
152
- expect(instance.a_method).to eq 42
153
- end
154
- end
155
-
156
- describe '#record_call' do
157
- it 'adds the given call to the list of calls' do
158
- double = described_class.new(
159
- build_world,
160
- :a_klass,
161
- :a_method,
162
- :an_implementation
163
- )
164
- double.record_call(:some_call)
165
- expect(double.calls.last).to eq :some_call
166
- end
167
- end
168
-
169
- describe '#call_original_method' do
170
- it 'binds the stored method object to the given object and calls it with the given args and block' do
171
- expected_args = [:one, :two, :three]
172
- expected_block = -> { }
173
- actual_args = actual_block = method_called = nil
174
- method_name = :a_method
175
- klass = create_class
176
- klass.__send__(:define_method, method_name) do |*args, &block|
177
- actual_args = args
178
- actual_block = block
179
- method_called = true
180
- end
181
- world = build_world(
182
- original_method_for: klass.instance_method(method_name)
183
- )
184
- instance = klass.new
185
- call = double('call',
186
- object: instance,
187
- method_name: method_name,
188
- args: expected_args,
189
- block: expected_block
190
- )
191
- double = described_class.new(
192
- world,
193
- klass,
194
- method_name,
195
- :an_implementation
196
- )
197
-
198
- double.activate
199
- double.call_original_method(call)
200
-
201
- expect(actual_args).to eq expected_args
202
- expect(actual_block).to eq expected_block
203
- expect(method_called).to eq true
204
- end
205
-
206
- it 'does nothing if no method has been stored' do
207
- method_name = :a_method
208
- world = build_world(original_method_for: nil)
209
- call = double('call', method_name: method_name)
210
- double = described_class.new(
211
- world,
212
- :klass,
213
- method_name,
214
- :an_implementation
215
- )
216
- expect { double.call_original_method(call) }.not_to raise_error
217
- end
218
-
219
- it 'does not store the original method multiple times when a method is doubled multiple times' do
220
- world = Shoulda::Matchers::Doublespeak::World.new
221
- klass = create_class(a_method: :some_return_value)
222
- method_name = :a_method
223
- doubles = 2.times.map do
224
- described_class.new(
225
- world,
226
- klass,
227
- method_name,
228
- build_implementation
229
- )
230
- end
231
- instance = klass.new
232
-
233
- doubles[0].activate
234
-
235
- was_called = false
236
- klass.__send__(:define_method, method_name) do
237
- was_called = true
238
- end
239
-
240
- doubles[1].activate
241
-
242
- doubles.each(&:deactivate)
243
-
244
- instance.__send__(method_name)
245
-
246
- expect(was_called).to be false
247
- end
248
- end
249
-
250
- def create_class(methods = {})
251
- Class.new.tap do |klass|
252
- methods.each do |name, value|
253
- klass.__send__(:define_method, name) { |*args| value }
254
- end
255
- end
256
- end
257
-
258
- def build_implementation
259
- double('implementation', returns: nil, call: nil)
260
- end
261
-
262
- def build_world(methods = {})
263
- defaults = {
264
- original_method_for: nil,
265
- store_original_method_for: nil,
266
- doubles_activated?: nil
267
- }
268
- double('world', defaults.merge(methods))
269
- end
270
- end
271
- end