shoulda-matchers 2.8.0 → 3.0.0.rc1

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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/.hound_config/ruby.yml +7 -0
  3. data/.travis.yml +11 -54
  4. data/Appraisals +45 -100
  5. data/CONTRIBUTING.md +51 -7
  6. data/Gemfile +7 -19
  7. data/Gemfile.lock +60 -134
  8. data/Guardfile +5 -0
  9. data/NEWS.md +203 -0
  10. data/README.md +95 -50
  11. data/Rakefile +1 -0
  12. data/doc_config/yard/templates/default/layout/html/setup.rb +1 -1
  13. data/gemfiles/4.0.0.gemfile +10 -7
  14. data/gemfiles/4.0.0.gemfile.lock +103 -79
  15. data/gemfiles/4.0.1.gemfile +10 -7
  16. data/gemfiles/4.0.1.gemfile.lock +109 -83
  17. data/gemfiles/4.1.gemfile +10 -7
  18. data/gemfiles/4.1.gemfile.lock +109 -85
  19. data/gemfiles/4.2.gemfile +10 -9
  20. data/gemfiles/4.2.gemfile.lock +86 -78
  21. data/lib/shoulda/matchers.rb +13 -18
  22. data/lib/shoulda/matchers/action_controller.rb +4 -1
  23. data/lib/shoulda/matchers/action_controller/flash_store.rb +95 -0
  24. data/lib/shoulda/matchers/action_controller/{strong_parameters_matcher.rb → permit_matcher.rb} +147 -30
  25. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +1 -1
  26. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +1 -1
  27. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +1 -1
  28. data/lib/shoulda/matchers/action_controller/rescue_from_matcher.rb +1 -1
  29. data/lib/shoulda/matchers/action_controller/route_matcher.rb +5 -1
  30. data/lib/shoulda/matchers/action_controller/route_params.rb +15 -6
  31. data/lib/shoulda/matchers/action_controller/session_store.rb +34 -0
  32. data/lib/shoulda/matchers/action_controller/set_flash_matcher.rb +30 -136
  33. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +28 -109
  34. data/lib/shoulda/matchers/action_controller/set_session_or_flash_matcher.rb +103 -0
  35. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +1 -12
  36. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +79 -10
  37. data/lib/shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher.rb +10 -0
  38. data/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb +21 -0
  39. data/lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb +24 -0
  40. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +22 -5
  41. data/lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb +29 -10
  42. data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +27 -10
  43. data/lib/shoulda/matchers/active_model/validate_length_of_matcher.rb +27 -12
  44. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +56 -20
  45. data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +3 -11
  46. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +65 -0
  47. data/lib/shoulda/matchers/active_record/association_matcher.rb +40 -6
  48. data/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +21 -7
  49. data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +11 -40
  50. data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +1 -1
  51. data/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb +2 -6
  52. data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +137 -22
  53. data/lib/shoulda/matchers/configuration.rb +20 -0
  54. data/lib/shoulda/matchers/doublespeak.rb +11 -1
  55. data/lib/shoulda/matchers/doublespeak/double.rb +29 -11
  56. data/lib/shoulda/matchers/doublespeak/double_collection.rb +4 -3
  57. data/lib/shoulda/matchers/doublespeak/method_call.rb +35 -0
  58. data/lib/shoulda/matchers/doublespeak/object_double.rb +7 -2
  59. data/lib/shoulda/matchers/doublespeak/proxy_implementation.rb +4 -3
  60. data/lib/shoulda/matchers/doublespeak/stub_implementation.rb +3 -3
  61. data/lib/shoulda/matchers/doublespeak/world.rb +21 -1
  62. data/lib/shoulda/matchers/integrations.rb +43 -0
  63. data/lib/shoulda/matchers/integrations/configuration.rb +68 -0
  64. data/lib/shoulda/matchers/integrations/configuration_error.rb +9 -0
  65. data/lib/shoulda/matchers/integrations/inclusion.rb +20 -0
  66. data/lib/shoulda/matchers/integrations/libraries.rb +15 -0
  67. data/lib/shoulda/matchers/integrations/libraries/action_controller.rb +31 -0
  68. data/lib/shoulda/matchers/integrations/libraries/active_model.rb +26 -0
  69. data/lib/shoulda/matchers/integrations/libraries/active_record.rb +26 -0
  70. data/lib/shoulda/matchers/integrations/libraries/missing_library.rb +19 -0
  71. data/lib/shoulda/matchers/integrations/libraries/rails.rb +30 -0
  72. data/lib/shoulda/matchers/integrations/rails.rb +12 -0
  73. data/lib/shoulda/matchers/integrations/registry.rb +28 -0
  74. data/lib/shoulda/matchers/integrations/test_frameworks.rb +16 -0
  75. data/lib/shoulda/matchers/integrations/test_frameworks/active_support_test_case.rb +37 -0
  76. data/lib/shoulda/matchers/integrations/test_frameworks/minitest_4.rb +36 -0
  77. data/lib/shoulda/matchers/integrations/test_frameworks/minitest_5.rb +37 -0
  78. data/lib/shoulda/matchers/integrations/test_frameworks/missing_test_framework.rb +40 -0
  79. data/lib/shoulda/matchers/integrations/test_frameworks/rspec.rb +29 -0
  80. data/lib/shoulda/matchers/integrations/test_frameworks/test_unit.rb +36 -0
  81. data/lib/shoulda/matchers/rails_shim.rb +0 -40
  82. data/lib/shoulda/matchers/version.rb +1 -1
  83. data/script/SUPPORTED_VERSIONS +1 -1
  84. data/script/update_gems_in_all_appraisals +14 -0
  85. data/shoulda-matchers.gemspec +2 -2
  86. data/spec/acceptance/active_model_integration_spec.rb +4 -1
  87. data/spec/acceptance/independent_matchers_spec.rb +6 -6
  88. data/spec/acceptance/multiple_libraries_integration_spec.rb +52 -0
  89. data/spec/acceptance/rails_integration_spec.rb +15 -5
  90. data/spec/acceptance_spec_helper.rb +8 -0
  91. data/spec/doublespeak_spec_helper.rb +14 -0
  92. data/spec/support/acceptance/adds_shoulda_matchers_to_project.rb +110 -0
  93. data/spec/support/acceptance/helpers.rb +2 -0
  94. data/spec/support/acceptance/helpers/base_helpers.rb +6 -1
  95. data/spec/support/acceptance/helpers/command_helpers.rb +6 -2
  96. data/spec/support/acceptance/helpers/minitest_helpers.rb +0 -8
  97. data/spec/support/acceptance/helpers/n_unit_helpers.rb +25 -0
  98. data/spec/support/acceptance/helpers/rspec_helpers.rb +2 -0
  99. data/spec/support/acceptance/helpers/step_helpers.rb +13 -19
  100. data/spec/support/acceptance/matchers/have_output.rb +1 -1
  101. data/spec/support/tests/bundle.rb +1 -1
  102. data/spec/support/tests/command_runner.rb +25 -13
  103. data/spec/support/tests/current_bundle.rb +47 -0
  104. data/spec/support/tests/database.rb +28 -0
  105. data/spec/support/tests/database_adapters/postgresql.rb +25 -0
  106. data/spec/support/tests/database_adapters/sqlite3.rb +26 -0
  107. data/spec/support/tests/database_configuration.rb +33 -0
  108. data/spec/support/tests/database_configuration_registry.rb +28 -0
  109. data/spec/support/tests/filesystem.rb +25 -2
  110. data/spec/support/unit/helpers/active_record_versions.rb +12 -0
  111. data/spec/support/unit/helpers/class_builder.rb +6 -2
  112. data/spec/support/unit/helpers/column_type_helpers.rb +26 -0
  113. data/spec/support/unit/helpers/controller_builder.rb +0 -28
  114. data/spec/support/unit/helpers/database_helpers.rb +18 -0
  115. data/spec/support/unit/helpers/model_builder.rb +38 -6
  116. data/spec/support/unit/helpers/rails_versions.rb +2 -2
  117. data/spec/support/unit/matchers/fail_with_message_including_matcher.rb +9 -8
  118. data/spec/support/unit/matchers/fail_with_message_matcher.rb +1 -1
  119. data/spec/support/unit/rails_application.rb +29 -13
  120. data/spec/support/unit/record_validating_confirmation_builder.rb +1 -2
  121. data/spec/support/unit/shared_examples/set_session_or_flash.rb +355 -0
  122. data/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb +433 -0
  123. data/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +1 -5
  124. data/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +37 -0
  125. data/spec/unit/shoulda/matchers/action_controller/set_flash_matcher_spec.rb +23 -147
  126. data/spec/unit/shoulda/matchers/action_controller/set_session_matcher_spec.rb +8 -285
  127. data/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb +562 -0
  128. data/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb +81 -14
  129. data/spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +16 -8
  130. data/spec/unit/shoulda/matchers/active_model/numericality_matchers/comparison_matcher_spec.rb +101 -9
  131. data/spec/unit/shoulda/matchers/active_model/numericality_matchers/even_number_matcher_spec.rb +39 -1
  132. data/spec/unit/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher_spec.rb +39 -1
  133. data/spec/unit/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher_spec.rb +39 -0
  134. data/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb +0 -17
  135. data/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +0 -17
  136. data/spec/unit/shoulda/matchers/active_model/validate_length_of_matcher_spec.rb +0 -17
  137. data/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +838 -271
  138. data/spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +0 -19
  139. data/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +93 -0
  140. data/spec/unit/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb +3 -3
  141. data/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb +25 -0
  142. data/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb +905 -0
  143. data/spec/unit/shoulda/matchers/doublespeak/double_collection_spec.rb +17 -11
  144. data/spec/unit/shoulda/matchers/doublespeak/double_implementation_registry_spec.rb +1 -1
  145. data/spec/unit/shoulda/matchers/doublespeak/double_spec.rb +144 -43
  146. data/spec/unit/shoulda/matchers/doublespeak/object_double_spec.rb +1 -1
  147. data/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb +36 -11
  148. data/spec/unit/shoulda/matchers/doublespeak/stub_implementation_spec.rb +29 -16
  149. data/spec/unit/shoulda/matchers/doublespeak/world_spec.rb +8 -5
  150. data/spec/unit/shoulda/matchers/doublespeak_spec.rb +1 -1
  151. data/spec/unit_spec_helper.rb +15 -14
  152. data/spec/warnings_spy.rb +1 -1
  153. metadata +68 -29
  154. data/docs.watchr +0 -5
  155. data/gemfiles/3.0.gemfile +0 -26
  156. data/gemfiles/3.0.gemfile.lock +0 -173
  157. data/gemfiles/3.1.gemfile +0 -32
  158. data/gemfiles/3.1.gemfile.lock +0 -212
  159. data/gemfiles/3.1_1.9.2.gemfile +0 -32
  160. data/gemfiles/3.1_1.9.2.gemfile.lock +0 -212
  161. data/gemfiles/3.2.gemfile +0 -33
  162. data/gemfiles/3.2.gemfile.lock +0 -212
  163. data/gemfiles/3.2_1.9.2.gemfile +0 -31
  164. data/gemfiles/3.2_1.9.2.gemfile.lock +0 -207
  165. data/lib/shoulda/matchers/assertion_error.rb +0 -27
  166. data/lib/shoulda/matchers/doublespeak/structs.rb +0 -10
  167. data/lib/shoulda/matchers/integrations/nunit_test_case_detection.rb +0 -39
  168. data/lib/shoulda/matchers/integrations/rspec.rb +0 -19
  169. data/lib/shoulda/matchers/integrations/test_unit.rb +0 -34
  170. data/spec/unit/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +0 -331
  171. data/spec/unit/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +0 -564
@@ -1,11 +1,11 @@
1
- require 'unit_spec_helper'
1
+ require 'doublespeak_spec_helper'
2
2
 
3
3
  module Shoulda::Matchers::Doublespeak
4
4
  describe DoubleCollection do
5
5
  describe '#register_stub' do
6
6
  it 'calls DoubleImplementationRegistry.find correctly' do
7
7
  allow(DoubleImplementationRegistry).to receive(:find)
8
- double_collection = described_class.new(:klass)
8
+ double_collection = described_class.new(build_world, :klass)
9
9
 
10
10
  double_collection.register_stub(:a_method)
11
11
 
@@ -13,24 +13,25 @@ module Shoulda::Matchers::Doublespeak
13
13
  end
14
14
 
15
15
  it 'calls Double.new correctly' do
16
+ world = build_world
16
17
  allow(DoubleImplementationRegistry).
17
18
  to receive(:find).
18
19
  and_return(:implementation)
19
20
  allow(Double).to receive(:new)
20
- double_collection = described_class.new(:klass)
21
+ double_collection = described_class.new(world, :klass)
21
22
 
22
23
  double_collection.register_stub(:a_method)
23
24
 
24
25
  expect(Double).
25
26
  to have_received(:new).
26
- with(:klass, :a_method, :implementation)
27
+ with(world, :klass, :a_method, :implementation)
27
28
  end
28
29
  end
29
30
 
30
31
  describe '#register_proxy' do
31
32
  it 'calls DoubleImplementationRegistry.find correctly' do
32
33
  allow(DoubleImplementationRegistry).to receive(:find)
33
- double_collection = described_class.new(:klass)
34
+ double_collection = described_class.new(build_world, :klass)
34
35
 
35
36
  double_collection.register_proxy(:a_method)
36
37
 
@@ -40,24 +41,25 @@ module Shoulda::Matchers::Doublespeak
40
41
  end
41
42
 
42
43
  it 'calls Double.new correctly' do
44
+ world = build_world
43
45
  allow(DoubleImplementationRegistry).
44
46
  to receive(:find).
45
47
  and_return(:implementation)
46
48
  allow(Double).to receive(:new)
47
- double_collection = described_class.new(:klass)
49
+ double_collection = described_class.new(world, :klass)
48
50
 
49
51
  double_collection.register_proxy(:a_method)
50
52
 
51
53
  expect(Double).
52
54
  to have_received(:new).
53
- with(:klass, :a_method, :implementation)
55
+ with(world, :klass, :a_method, :implementation)
54
56
  end
55
57
  end
56
58
 
57
59
  describe '#activate' do
58
60
  it 'replaces all registered methods with doubles' do
59
61
  klass = create_class(first_method: 1, second_method: 2)
60
- double_collection = described_class.new(klass)
62
+ double_collection = described_class.new(build_world, klass)
61
63
  double_collection.register_stub(:first_method)
62
64
  double_collection.register_stub(:second_method)
63
65
 
@@ -72,7 +74,7 @@ module Shoulda::Matchers::Doublespeak
72
74
  describe '#deactivate' do
73
75
  it 'restores the original methods that were doubled' do
74
76
  klass = create_class(first_method: 1, second_method: 2)
75
- double_collection = described_class.new(klass)
77
+ double_collection = described_class.new(build_world, klass)
76
78
  double_collection.register_stub(:first_method)
77
79
  double_collection.register_stub(:second_method)
78
80
 
@@ -88,7 +90,7 @@ module Shoulda::Matchers::Doublespeak
88
90
  describe '#calls_to' do
89
91
  it 'returns all calls to the given method' do
90
92
  klass = create_class(a_method: nil)
91
- double_collection = described_class.new(klass)
93
+ double_collection = described_class.new(build_world, klass)
92
94
  double_collection.register_stub(:a_method)
93
95
  double_collection.activate
94
96
 
@@ -108,7 +110,7 @@ module Shoulda::Matchers::Doublespeak
108
110
 
109
111
  it 'returns an empty array if the method has never been doubled' do
110
112
  klass = create_class
111
- double_collection = described_class.new(klass)
113
+ double_collection = described_class.new(build_world, klass)
112
114
  expect(double_collection.calls_to(:non_existent_method)).to eq []
113
115
  end
114
116
  end
@@ -120,5 +122,9 @@ module Shoulda::Matchers::Doublespeak
120
122
  end
121
123
  end
122
124
  end
125
+
126
+ def build_world
127
+ Shoulda::Matchers::Doublespeak::World.new
128
+ end
123
129
  end
124
130
  end
@@ -1,4 +1,4 @@
1
- require 'unit_spec_helper'
1
+ require 'doublespeak_spec_helper'
2
2
 
3
3
  module Shoulda::Matchers::Doublespeak
4
4
  describe DoubleImplementationRegistry do
@@ -1,4 +1,4 @@
1
- require 'unit_spec_helper'
1
+ require 'doublespeak_spec_helper'
2
2
 
3
3
  module Shoulda::Matchers::Doublespeak
4
4
  describe Double do
@@ -11,14 +11,24 @@ module Shoulda::Matchers::Doublespeak
11
11
  implementation.singleton_class.__send__(:define_method, :returns) do |&block|
12
12
  actual_block = block
13
13
  end
14
- double = described_class.new(:klass, :a_method, implementation)
14
+ double = described_class.new(
15
+ :a_world,
16
+ :klass,
17
+ :a_method,
18
+ implementation
19
+ )
15
20
  double.to_return(&sent_block)
16
21
  expect(actual_block).to eq sent_block
17
22
  end
18
23
 
19
24
  it 'tells its implementation to return the given value' do
20
25
  implementation = build_implementation
21
- double = described_class.new(:klass, :a_method, implementation)
26
+ double = described_class.new(
27
+ :a_world,
28
+ :klass,
29
+ :a_method,
30
+ implementation
31
+ )
22
32
  double.to_return(:implementation)
23
33
 
24
34
  expect(implementation).to have_received(:returns).with(:implementation)
@@ -32,7 +42,12 @@ module Shoulda::Matchers::Doublespeak
32
42
  implementation.singleton_class.__send__(:define_method, :returns) do |&block|
33
43
  actual_block = block
34
44
  end
35
- double = described_class.new(:klass, :a_method, implementation)
45
+ double = described_class.new(
46
+ :a_world,
47
+ :klass,
48
+ :a_method,
49
+ implementation
50
+ )
36
51
  double.to_return(:value, &sent_block)
37
52
  expect(actual_block).to eq sent_block
38
53
  end
@@ -41,27 +56,47 @@ module Shoulda::Matchers::Doublespeak
41
56
  describe '#activate' do
42
57
  it 'replaces the method with an implementation' do
43
58
  implementation = build_implementation
44
- klass = create_class(a_method: 42)
59
+ method_name = :a_method
60
+ klass = create_class(method_name => :some_return_value)
45
61
  instance = klass.new
46
- double = described_class.new(klass, :a_method, implementation)
62
+ double = described_class.new(
63
+ build_world,
64
+ klass,
65
+ method_name,
66
+ implementation
67
+ )
47
68
  args = [:any, :args]
48
69
  block = -> {}
70
+ call = MethodCall.new(
71
+ double: double,
72
+ object: instance,
73
+ method_name: method_name,
74
+ args: args,
75
+ block: block
76
+ )
49
77
 
50
78
  double.activate
51
- instance.a_method(*args, &block)
79
+ instance.__send__(method_name, *args, &block)
52
80
 
53
81
  expect(implementation).
54
82
  to have_received(:call).
55
- with(double, instance, args, block)
83
+ with(call)
56
84
  end
57
85
  end
58
86
 
59
87
  describe '#deactivate' do
60
88
  it 'restores the original method after being doubled' do
61
- implementation = build_implementation
62
89
  klass = create_class(a_method: 42)
90
+ world = build_world(
91
+ original_method_for: klass.instance_method(:a_method)
92
+ )
63
93
  instance = klass.new
64
- double = described_class.new(klass, :a_method, implementation)
94
+ double = described_class.new(
95
+ world,
96
+ klass,
97
+ :a_method,
98
+ build_implementation
99
+ )
65
100
 
66
101
  double.activate
67
102
  double.deactivate
@@ -69,10 +104,18 @@ module Shoulda::Matchers::Doublespeak
69
104
  end
70
105
 
71
106
  it 'still restores the original method if #activate was called twice' do
72
- implementation = build_implementation
73
- klass = create_class(a_method: 42)
107
+ method_name = :a_method
108
+ klass = create_class(method_name => 42)
109
+ world = build_world(
110
+ original_method_for: klass.instance_method(:a_method)
111
+ )
74
112
  instance = klass.new
75
- double = described_class.new(klass, :a_method, implementation)
113
+ double = described_class.new(
114
+ world,
115
+ klass,
116
+ :a_method,
117
+ build_implementation
118
+ )
76
119
 
77
120
  double.activate
78
121
  double.activate
@@ -81,10 +124,14 @@ module Shoulda::Matchers::Doublespeak
81
124
  end
82
125
 
83
126
  it 'does nothing if the method has not been doubled' do
84
- implementation = build_implementation
85
127
  klass = create_class(a_method: 42)
86
128
  instance = klass.new
87
- double = described_class.new(klass, :a_method, implementation)
129
+ double = described_class.new(
130
+ :a_world,
131
+ klass,
132
+ :a_method,
133
+ build_implementation
134
+ )
88
135
 
89
136
  double.deactivate
90
137
  expect(instance.a_method).to eq 42
@@ -92,50 +139,96 @@ module Shoulda::Matchers::Doublespeak
92
139
  end
93
140
 
94
141
  describe '#record_call' do
95
- it 'stores the arguments and block given to the method in calls' do
96
- double = described_class.new(:klass, :a_method, :implementation)
97
- calls = [
98
- [:any, :args], :block,
99
- [:more, :args]
100
- ]
101
- double.record_call(calls[0][0], calls[0][1])
102
- double.record_call(calls[1][0], nil)
103
-
104
- expect(double.calls[0].args).to eq calls[0][0]
105
- expect(double.calls[0].block).to eq calls[0][1]
106
- expect(double.calls[1].args).to eq calls[1][0]
142
+ it 'adds the given call to the list of calls' do
143
+ double = described_class.new(
144
+ :a_world,
145
+ :a_klass,
146
+ :a_method,
147
+ :an_implementation
148
+ )
149
+ double.record_call(:some_call)
150
+ expect(double.calls.last).to eq :some_call
107
151
  end
108
152
  end
109
153
 
110
154
  describe '#call_original_method' do
111
- it 'binds the stored method object to the class and calls it with the given args and block' do
112
- klass = create_class
113
- instance = klass.new
114
- actual_args = actual_block = method_called = nil
155
+ it 'binds the stored method object to the given object and calls it with the given args and block' do
115
156
  expected_args = [:one, :two, :three]
116
157
  expected_block = -> { }
117
- double = described_class.new(klass, :a_method, :implementation)
118
-
119
- klass.__send__(:define_method, :a_method) do |*args, &block|
120
- actual_args = expected_args
121
- actual_block = expected_block
158
+ actual_args = actual_block = method_called = nil
159
+ method_name = :a_method
160
+ klass = create_class
161
+ klass.__send__(:define_method, method_name) do |*args, &block|
162
+ actual_args = args
163
+ actual_block = block
122
164
  method_called = true
123
165
  end
166
+ world = build_world(
167
+ original_method_for: klass.instance_method(method_name)
168
+ )
169
+ instance = klass.new
170
+ call = double('call',
171
+ object: instance,
172
+ method_name: method_name,
173
+ args: expected_args,
174
+ block: expected_block
175
+ )
176
+ double = described_class.new(
177
+ world,
178
+ klass,
179
+ method_name,
180
+ :an_implementation
181
+ )
124
182
 
125
183
  double.activate
126
- double.call_original_method(instance, expected_args, expected_block)
184
+ double.call_original_method(call)
127
185
 
128
- expect(expected_args).to eq actual_args
129
- expect(expected_block).to eq actual_block
186
+ expect(actual_args).to eq expected_args
187
+ expect(actual_block).to eq expected_block
130
188
  expect(method_called).to eq true
131
189
  end
132
190
 
133
191
  it 'does nothing if no method has been stored' do
134
- double = described_class.new(:klass, :a_method, :implementation)
192
+ method_name = :a_method
193
+ world = build_world(original_method_for: nil)
194
+ call = double('call', method_name: method_name)
195
+ double = described_class.new(
196
+ world,
197
+ :klass,
198
+ method_name,
199
+ :an_implementation
200
+ )
201
+ expect { double.call_original_method(call) }.not_to raise_error
202
+ end
203
+
204
+ it 'does not store the original method multiple times when a method is doubled multiple times' do
205
+ world = Shoulda::Matchers::Doublespeak::World.new
206
+ klass = create_class(a_method: :some_return_value)
207
+ method_name = :a_method
208
+ doubles = 2.times.map do
209
+ described_class.new(
210
+ world,
211
+ klass,
212
+ method_name,
213
+ build_implementation
214
+ )
215
+ end
216
+ instance = klass.new
217
+
218
+ doubles[0].activate
219
+
220
+ was_called = false
221
+ klass.__send__(:define_method, method_name) do
222
+ was_called = true
223
+ end
224
+
225
+ doubles[1].activate
226
+
227
+ doubles.each(&:deactivate)
135
228
 
136
- expect {
137
- double.call_original_method(:instance, [:any, :args], nil)
138
- }.not_to raise_error
229
+ instance.__send__(method_name)
230
+
231
+ expect(was_called).to be false
139
232
  end
140
233
  end
141
234
 
@@ -150,5 +243,13 @@ module Shoulda::Matchers::Doublespeak
150
243
  def build_implementation
151
244
  double('implementation', returns: nil, call: nil)
152
245
  end
246
+
247
+ def build_world(methods = {})
248
+ defaults = {
249
+ original_method_for: nil,
250
+ store_original_method_for: nil
251
+ }
252
+ double('world', defaults.merge(methods))
253
+ end
153
254
  end
154
255
  end
@@ -1,4 +1,4 @@
1
- require 'unit_spec_helper'
1
+ require 'doublespeak_spec_helper'
2
2
 
3
3
  module Shoulda::Matchers::Doublespeak
4
4
  describe ObjectDouble do
@@ -1,4 +1,4 @@
1
- require 'unit_spec_helper'
1
+ require 'doublespeak_spec_helper'
2
2
 
3
3
  module Shoulda::Matchers::Doublespeak
4
4
  describe ProxyImplementation do
@@ -13,26 +13,45 @@ module Shoulda::Matchers::Doublespeak
13
13
  end
14
14
 
15
15
  describe '#call' do
16
+ it 'calls #call_original_method on the double' do
17
+ stub_implementation = build_stub_implementation
18
+ double = build_double
19
+ call = build_call(double: double)
20
+ allow(double).to receive(:call_original_method)
21
+ implementation = described_class.new(stub_implementation)
22
+
23
+ implementation.call(call)
24
+
25
+ expect(double).
26
+ to have_received(:call_original_method).
27
+ with(call)
28
+ end
29
+
16
30
  it 'delegates to its stub_implementation' do
17
31
  stub_implementation = build_stub_implementation
18
32
  double = build_double
33
+ call2 = build_call
34
+ call = build_call(double: double, with_return_value: call2)
35
+ allow(double).to receive(:call_original_method)
19
36
  implementation = described_class.new(stub_implementation)
20
- implementation.call(double, :object, :args, :block)
37
+
38
+ implementation.call(call)
21
39
 
22
40
  expect(stub_implementation).
23
41
  to have_received(:call).
24
- with(double, :object, :args, :block)
42
+ with(call2)
25
43
  end
26
44
 
27
- it 'calls #call_original_method on the double' do
45
+ it 'returns the return value of the original method' do
46
+ return_value = :some_return_value
28
47
  stub_implementation = build_stub_implementation
48
+ double = build_double(call_original_method: return_value)
49
+ call = build_call(double: double)
29
50
  implementation = described_class.new(stub_implementation)
30
- double = build_double
31
- implementation.call(double, :object, :args, :block)
32
51
 
33
- expect(double).
34
- to have_received(:call_original_method).
35
- with(:object, :args, :block)
52
+ return_value = implementation.call(call)
53
+
54
+ expect(return_value).to be return_value
36
55
  end
37
56
  end
38
57
 
@@ -40,8 +59,14 @@ module Shoulda::Matchers::Doublespeak
40
59
  double('stub_implementation', returns: nil, call: nil)
41
60
  end
42
61
 
43
- def build_double
44
- double('double', call_original_method: nil)
62
+ def build_double(methods = {})
63
+ defaults = { call_original_method: nil }
64
+ double('double', defaults.merge(methods))
65
+ end
66
+
67
+ def build_call(methods = {})
68
+ defaults = { double: build_double, with_return_value: nil }
69
+ double('call', defaults.merge(methods))
45
70
  end
46
71
  end
47
72
  end