rspec-mocks 3.0.0.beta1 → 3.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. data.tar.gz.sig +1 -1
  2. data/Changelog.md +95 -3
  3. data/README.md +27 -13
  4. data/features/README.md +15 -7
  5. data/features/argument_matchers/README.md +5 -5
  6. data/features/argument_matchers/explicit.feature +6 -6
  7. data/features/argument_matchers/general_matchers.feature +4 -4
  8. data/features/argument_matchers/type_matchers.feature +2 -2
  9. data/features/message_expectations/README.md +29 -27
  10. data/features/message_expectations/call_original.feature +0 -1
  11. data/features/message_expectations/message_chains_using_expect.feature +49 -0
  12. data/features/method_stubs/README.md +2 -1
  13. data/features/method_stubs/{any_instance.feature → allow_any_instance_of.feature} +12 -12
  14. data/features/method_stubs/{stub_chain.feature → receive_message_chain.feature} +3 -3
  15. data/features/method_stubs/to_ary.feature +1 -1
  16. data/features/mutating_constants/stub_defined_constant.feature +0 -1
  17. data/features/outside_rspec/standalone.feature +1 -1
  18. data/features/spies/spy_pure_mock_method.feature +1 -1
  19. data/features/test_frameworks/test_unit.feature +21 -10
  20. data/features/verifying_doubles/README.md +17 -0
  21. data/features/verifying_doubles/class_doubles.feature +1 -16
  22. data/features/verifying_doubles/dynamic_classes.feature +0 -1
  23. data/features/verifying_doubles/{introduction.feature → instance_doubles.feature} +41 -23
  24. data/features/verifying_doubles/partial_doubles.feature +2 -2
  25. data/lib/rspec/mocks.rb +69 -82
  26. data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +35 -0
  27. data/lib/rspec/mocks/any_instance/expectation_chain.rb +1 -2
  28. data/lib/rspec/mocks/any_instance/recorder.rb +52 -18
  29. data/lib/rspec/mocks/any_instance/stub_chain.rb +1 -1
  30. data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +4 -0
  31. data/lib/rspec/mocks/argument_list_matcher.rb +10 -44
  32. data/lib/rspec/mocks/argument_matchers.rb +132 -163
  33. data/lib/rspec/mocks/configuration.rb +28 -4
  34. data/lib/rspec/mocks/error_generator.rb +46 -13
  35. data/lib/rspec/mocks/example_methods.rb +13 -12
  36. data/lib/rspec/mocks/extensions/marshal.rb +1 -1
  37. data/lib/rspec/mocks/framework.rb +3 -4
  38. data/lib/rspec/mocks/instance_method_stasher.rb +2 -3
  39. data/lib/rspec/mocks/matchers/have_received.rb +8 -6
  40. data/lib/rspec/mocks/matchers/receive.rb +28 -20
  41. data/lib/rspec/mocks/matchers/receive_message_chain.rb +65 -0
  42. data/lib/rspec/mocks/matchers/receive_messages.rb +3 -2
  43. data/lib/rspec/mocks/message_chain.rb +91 -0
  44. data/lib/rspec/mocks/message_expectation.rb +86 -80
  45. data/lib/rspec/mocks/method_double.rb +2 -11
  46. data/lib/rspec/mocks/method_reference.rb +82 -23
  47. data/lib/rspec/mocks/method_signature_verifier.rb +207 -0
  48. data/lib/rspec/mocks/mutate_const.rb +34 -50
  49. data/lib/rspec/mocks/object_reference.rb +0 -1
  50. data/lib/rspec/mocks/proxy.rb +70 -13
  51. data/lib/rspec/mocks/ruby_features.rb +24 -0
  52. data/lib/rspec/mocks/space.rb +105 -31
  53. data/lib/rspec/mocks/standalone.rb +2 -2
  54. data/lib/rspec/mocks/syntax.rb +43 -8
  55. data/lib/rspec/mocks/targets.rb +16 -7
  56. data/lib/rspec/mocks/test_double.rb +41 -15
  57. data/lib/rspec/mocks/verifying_double.rb +51 -4
  58. data/lib/rspec/mocks/verifying_message_expecation.rb +12 -12
  59. data/lib/rspec/mocks/verifying_proxy.rb +32 -19
  60. data/lib/rspec/mocks/version.rb +1 -1
  61. data/spec/rspec/mocks/and_call_original_spec.rb +28 -7
  62. data/spec/rspec/mocks/and_return_spec.rb +23 -0
  63. data/spec/rspec/mocks/and_yield_spec.rb +1 -2
  64. data/spec/rspec/mocks/any_instance_spec.rb +33 -17
  65. data/spec/rspec/mocks/array_including_matcher_spec.rb +6 -6
  66. data/spec/rspec/mocks/before_all_spec.rb +132 -0
  67. data/spec/rspec/mocks/block_return_value_spec.rb +12 -1
  68. data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +9 -11
  69. data/spec/rspec/mocks/configuration_spec.rb +14 -1
  70. data/spec/rspec/mocks/double_spec.rb +867 -24
  71. data/spec/rspec/mocks/example_methods_spec.rb +13 -0
  72. data/spec/rspec/mocks/extensions/marshal_spec.rb +17 -17
  73. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +29 -1
  74. data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +12 -12
  75. data/spec/rspec/mocks/hash_including_matcher_spec.rb +21 -17
  76. data/spec/rspec/mocks/instance_method_stasher_spec.rb +2 -3
  77. data/spec/rspec/mocks/matchers/have_received_spec.rb +7 -0
  78. data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +198 -0
  79. data/spec/rspec/mocks/matchers/receive_messages_spec.rb +2 -2
  80. data/spec/rspec/mocks/matchers/receive_spec.rb +19 -6
  81. data/spec/rspec/mocks/method_signature_verifier_spec.rb +272 -0
  82. data/spec/rspec/mocks/methods_spec.rb +0 -1
  83. data/spec/rspec/mocks/multiple_return_value_spec.rb +2 -2
  84. data/spec/rspec/mocks/mutate_const_spec.rb +24 -1
  85. data/spec/rspec/mocks/nil_expectation_warning_spec.rb +6 -22
  86. data/spec/rspec/mocks/null_object_mock_spec.rb +13 -7
  87. data/spec/rspec/mocks/options_hash_spec.rb +3 -3
  88. data/spec/rspec/mocks/{partial_mock_spec.rb → partial_double_spec.rb} +5 -2
  89. data/spec/rspec/mocks/{partial_mock_using_mocks_directly_spec.rb → partial_double_using_mocks_directly_spec.rb} +1 -1
  90. data/spec/rspec/mocks/passing_argument_matchers_spec.rb +18 -0
  91. data/spec/rspec/mocks/serialization_spec.rb +1 -0
  92. data/spec/rspec/mocks/space_spec.rb +218 -7
  93. data/spec/rspec/mocks/stub_chain_spec.rb +6 -0
  94. data/spec/rspec/mocks/stub_spec.rb +0 -6
  95. data/spec/rspec/mocks/syntax_spec.rb +19 -0
  96. data/spec/rspec/mocks/test_double_spec.rb +0 -1
  97. data/spec/rspec/mocks/verifying_double_spec.rb +281 -18
  98. data/spec/rspec/mocks/verifying_message_expecation_spec.rb +7 -6
  99. data/spec/rspec/mocks_spec.rb +168 -42
  100. data/spec/spec_helper.rb +34 -22
  101. metadata +94 -63
  102. metadata.gz.sig +0 -0
  103. checksums.yaml +0 -15
  104. checksums.yaml.gz.sig +0 -2
  105. data/features/outside_rspec/configuration.feature +0 -60
  106. data/lib/rspec/mocks/arity_calculator.rb +0 -66
  107. data/lib/rspec/mocks/errors.rb +0 -12
  108. data/lib/rspec/mocks/mock.rb +0 -7
  109. data/lib/rspec/mocks/proxy_for_nil.rb +0 -37
  110. data/lib/rspec/mocks/stub_chain.rb +0 -51
  111. data/spec/rspec/mocks/argument_expectation_spec.rb +0 -32
  112. data/spec/rspec/mocks/arity_calculator_spec.rb +0 -95
  113. data/spec/rspec/mocks/mock_space_spec.rb +0 -113
  114. data/spec/rspec/mocks/mock_spec.rb +0 -788
data.tar.gz.sig CHANGED
@@ -1 +1 @@
1
- �旺]v�����h��( *�z(��������:�d-XH�?ibI4L��0*�ʆ˫K��Y?D��u<��);�p��]8�ҥ��P��8�ۥ��v<�MZM@���3��ӑ۶�Y���Ͷ�D�ОP �Mɭ�(B�[�Vk�"8��)'�Hs�%�jldyg�����Ԅ�f��đ��)�ѻ�gb�L��K��;7r)գC�1�� lNvKϣ!���Qr$9�P�ZIu'�a��=[�6/e
1
+ �Y;�Ԝ��q�(^g>��2�B�äh��'�G��~���C�7H��d�Cҷ���ΗS�ݛʭ�a����^E�+�P�\#]�=] FhIx��4�s
@@ -1,3 +1,70 @@
1
+ ### 3.0.0.beta2 / 2014-02-17
2
+ [full changelog](http://github.com/rspec/rspec-mocks/compare/v3.0.0.beta1...v3.0.0.beta2)
3
+
4
+ Breaking Changes for 3.0.0:
5
+
6
+ * Rename `RSpec::Mocks::Mock` to `RSpec::Mocks::Double`. (Myron Marston)
7
+ * Change how to integrate rspec-mocks in other test frameworks. You now
8
+ need to include `RSpec::Mocks::ExampleMethods` in your test context.
9
+ (Myron Marston)
10
+ * Prevent RSpec mocks' doubles and partial doubles from being used outside of
11
+ the per-test lifecycle (e.g. from a `before(:all)` hook). (Sam Phippen)
12
+ * Remove the `host` argument of `RSpec::Mocks.setup`. Instead
13
+ `RSpec::Mocks::ExampleMethods` should be included directly in the scope where
14
+ RSpec's mocking capabilities are used. (Sam Phippen)
15
+ * Make test doubles raise errors if you attempt to use them after they
16
+ get reset, to help surface issues when you accidentally retain
17
+ references to test doubles and attempt to reuse them in another
18
+ example. (Myron Marston)
19
+ * Remove support for `and_return { value }` and `and_return` without arguments. (Yuji Nakayama)
20
+
21
+ Enhancements:
22
+
23
+ * Add `receive_message_chain` which provides the functionality of the old
24
+ `stub_chain` for the new allow/expect syntax. Use it like so: `allow(...).to
25
+ receive_message_chain(:foo, :bar, :bazz)`. (Sam Phippen).
26
+ * Change argument matchers to use `===` as their primary matching
27
+ protocol, since their semantics mirror that of a case or rescue statement
28
+ (which uses `===` for matching). (Myron Marston)
29
+ * Add `RSpec::Mocks.with_temporary_scope`, which allows you to create
30
+ temporary rspec-mocks scopes in arbitrary places (such as a
31
+ `before(:all)` hook). (Myron Marston)
32
+ * Support keyword arguments when checking arity with verifying doubles.
33
+ (Xavier Shay)
34
+
35
+ Bug Fixes:
36
+
37
+ * Fix regression in 3.0.0.beta1 that caused `double("string_name" => :value)`
38
+ to stop working. (Xavier Shay)
39
+ * Fix the way rspec-mocks and rspec-core interact so that if users
40
+ define a `let` with the same name as one of the methods
41
+ from `RSpec::Mocks::ArgumentMatchers`, the user's `let` takes
42
+ precedence. (Michi Huber, Myron Marston)
43
+ * Fix verified doubles so that their methods match the visibility
44
+ (public, protected or private) of the interface they verify
45
+ against. (Myron Marston)
46
+ * Fix verified null object doubles so that they do not wrongly
47
+ report that they respond to anything. They only respond to methods
48
+ available on the interface they verify against. (Myron Marston)
49
+ * Fix deprecation warning for use of old `:should` syntax w/o explicit
50
+ config so that it no longer is silenced by an extension gem such
51
+ as rspec-rails when it calls `config.add_stub_and_should_receive_to`.
52
+ (Sam Phippen)
53
+ * Fix `expect` syntax so that it does not wrongly emit a "You're
54
+ overriding a previous implementation for this stub" warning when
55
+ you are not actually doing that. (Myron Marston)
56
+ * Fix `any_instance.unstub` when used on sub classes for whom the super
57
+ class has had `any_instance.stub` invoked on. (Jon Rowe)
58
+ * Fix regression in `stub_chain`/`receive_message_chain` that caused
59
+ it to raise an `ArgumentError` when passing args to the stubbed
60
+ methods. (Sam Phippen)
61
+ * Correct stub of undefined parent modules all the way down when stubbing a
62
+ nested constant. (Xavier Shay)
63
+ * Raise `VerifyingDoubleNotDefinedError` when a constant is not defined for
64
+ a verifying class double. (Maurício Linhares)
65
+ * Remove `Double#to_str`, which caused confusing `raise some_double`
66
+ behavior. (Maurício Linhares)
67
+
1
68
  ### 3.0.0.beta1 / 2013-11-07
2
69
  [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.99.0.beta1...v3.0.0.beta1)
3
70
 
@@ -31,8 +98,8 @@ Enhancements:
31
98
  (Xavier Shay)
32
99
  * Provide `object_double` to create verified doubles of specific object
33
100
  instances. (Xavier Shay)
34
- * Provide 'verify_partial_doubles` configuration that provides `object_double`
35
- like verification behaviour on partial mocks. (Xavier Shay)
101
+ * Provide `verify_partial_doubles` configuration that provides `object_double`
102
+ like verification behaviour on partial doubles. (Xavier Shay)
36
103
  * Improved performance of double creation, particularly those with many
37
104
  attributes. (Xavier Shay)
38
105
  * Default value of `transfer_nested_constants` option for constant stubbing can
@@ -50,7 +117,7 @@ Enhancements:
50
117
  Deprecations:
51
118
 
52
119
  * Using the old `:should` syntax without explicitly configuring it
53
- is disabled. It will continue to work but will emit a deprecation
120
+ is deprecated. It will continue to work but will emit a deprecation
54
121
  warning in RSpec 3 if you do not explicitly enable it. (Sam Phippen)
55
122
 
56
123
  Bug Fixes:
@@ -62,6 +129,21 @@ Bug Fixes:
62
129
  returns `nil` or `''` so that you still get a useful message.
63
130
  (Nick DeLuca)
64
131
 
132
+ ### 2.99.0.beta2 / 2014-02-17
133
+ [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.99.0.beta1...v2.99.0.beta2)
134
+
135
+ Deprecations:
136
+
137
+ * Deprecate `RSpec::Mocks::Mock` in favor of `RSpec::Mocks::Double`.
138
+ (Myron Marston)
139
+ * Deprecate the `host` argument of `RSpec::Mocks.setup`. Instead
140
+ `RSpec::Mocks::ExampleMethods` should be included directly in the scope where
141
+ RSpec's mocking capabilities are used. (Sam Phippen)
142
+ * Deprecate using any of rspec-mocks' features outside the per-test
143
+ lifecycle (e.g. from a `before(:all)` hook). (Myron Marston)
144
+ * Deprecate re-using a test double in another example. (Myron Marston)
145
+ * Deprecate `and_return { value }` and `and_return` without arguments. (Yuji Nakayama)
146
+
65
147
  ### 2.99.0.beta1 / 2013-11-07
66
148
  [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.14.4...v2.99.0.beta1)
67
149
 
@@ -87,6 +169,16 @@ Enhancements:
87
169
  * Add a config option to yield the receiver to `any_instance` implementation
88
170
  blocks. (Sam Phippen)
89
171
 
172
+ ### 2.14.5 / 2014-02-01
173
+ [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.14.4...v2.14.5)
174
+
175
+ Bug Fixes:
176
+
177
+ * Fix regression that caused block implementations to not receive all
178
+ args on 1.8.7 if the block also receives a block, due to Proc#arity
179
+ reporting `1` no matter how many args the block receives if it
180
+ receives a block, too. (Myron Marston)
181
+
90
182
  ### 2.14.4 / 2013-10-15
91
183
  [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.14.3...v2.14.4)
92
184
 
data/README.md CHANGED
@@ -11,14 +11,28 @@ alike.
11
11
 
12
12
  ## Test Doubles
13
13
 
14
- A Test Double is an object that stands in for a real object in a test.
15
- RSpec creates test doubles that support method stubs and message
16
- expectations.
14
+ A test double is an object that stands in for another object in your system
15
+ during a code example. Use the `double` method, passing in an optional identifier, to create one:
17
16
 
18
17
  ```ruby
19
18
  book = double("book")
20
19
  ```
21
20
 
21
+ Most of the time you will want some confidence that your doubles resemble an
22
+ existing object in your system. Verifying doubles are provided for this
23
+ purpose. If the existing object is available, they will prevent you from adding
24
+ stubs and expectations for methods that do not exist or that have an invalid
25
+ number of parameters.
26
+
27
+ ```ruby
28
+ book = instance_double("Book", :pages => 250)
29
+ ```
30
+
31
+ Verifying doubles have some clever tricks to enable you to both test in
32
+ isolation without your dependencies loaded while still being able to validate
33
+ them against real objects. More detail is available in [their
34
+ documentation](https://github.com/rspec/rspec-mocks/blob/master/features/verifying_doubles).
35
+
22
36
  ## Method Stubs
23
37
 
24
38
  A method stub is an implementation that returns a pre-determined value. Method
@@ -49,7 +63,7 @@ This is particularly nice when providing a list of test doubles to a method
49
63
  that iterates through them:
50
64
 
51
65
  ```ruby
52
- order.calculate_total_price(double(:price => 1.99),double(:price => 2.99))
66
+ order.calculate_total_price(double(:price => 1.99), double(:price => 2.99))
53
67
  ```
54
68
 
55
69
  ## Consecutive return values
@@ -59,7 +73,7 @@ arguments to `and_return`. The invocations cycle through the list. The last
59
73
  value is returned for any subsequent invocations:
60
74
 
61
75
  ```ruby
62
- allow(die).to receive(:roll).and_return(1,2,3)
76
+ allow(die).to receive(:roll).and_return(1, 2, 3)
63
77
  die.roll # => 1
64
78
  die.roll # => 2
65
79
  die.roll # => 3
@@ -125,7 +139,7 @@ generic kind of object: a Test Double.
125
139
 
126
140
  ### Test-Specific Extension
127
141
 
128
- a.k.a. Partial Stub/Mock, a Test-Specific Extension is an extension of a
142
+ a.k.a. Partial Double, a Test-Specific Extension is an extension of a
129
143
  real object in a system that is instrumented with test-double like
130
144
  behaviour in the context of a test. This technique is very common in Ruby
131
145
  because we often see class objects acting as global namespaces for methods.
@@ -206,7 +220,7 @@ expect(double).to receive(:msg).any_number_of_times
206
220
  ```ruby
207
221
  expect(double).to receive(:msg).ordered
208
222
  expect(double).to receive(:other_msg).ordered
209
- #This will fail if the messages are received out of order
223
+ # This will fail if the messages are received out of order
210
224
  ```
211
225
 
212
226
  This can include the same message with different arguments:
@@ -234,11 +248,11 @@ expect(double).to receive(:msg).and_return(value)
234
248
  expect(double).to receive(:msg).exactly(3).times.and_return(value1, value2, value3)
235
249
  # returns value1 the first time, value2 the second, etc
236
250
  expect(double).to receive(:msg).and_raise(error)
237
- #error can be an instantiated object or a class
238
- #if it is a class, it must be instantiable with no args
251
+ # error can be an instantiated object or a class
252
+ # if it is a class, it must be instantiable with no args
239
253
  expect(double).to receive(:msg).and_throw(:msg)
240
- expect(double).to receive(:msg).and_yield(values,to,yield)
241
- expect(double).to receive(:msg).and_yield(values,to,yield).and_yield(some,other,values,this,time)
254
+ expect(double).to receive(:msg).and_yield(values, to, yield)
255
+ expect(double).to receive(:msg).and_yield(values, to, yield).and_yield(some, other, values, this, time)
242
256
  # for methods that yield to a block multiple times
243
257
  ```
244
258
 
@@ -249,8 +263,8 @@ allow(double).to receive(:msg).and_return(value)
249
263
  allow(double).to receive(:msg).and_return(value1, value2, value3)
250
264
  allow(double).to receive(:msg).and_raise(error)
251
265
  allow(double).to receive(:msg).and_throw(:msg)
252
- allow(double).to receive(:msg).and_yield(values,to,yield)
253
- allow(double).to receive(:msg).and_yield(values,to,yield).and_yield(some,other,values,this,time)
266
+ allow(double).to receive(:msg).and_yield(values, to, yield)
267
+ allow(double).to receive(:msg).and_yield(values, to, yield).and_yield(some, other, values, this, time)
254
268
  ```
255
269
 
256
270
  ## Arbitrary Handling
@@ -15,20 +15,28 @@ invokes a _method_ with the same name as the message.
15
15
  ## Test Doubles
16
16
 
17
17
  A test double is an object that stands in for another object in your system
18
- during a code example. Use the `double` method to create one:
18
+ during a code example. Use the `double` method, passing in an optional identifier, to create one:
19
19
 
20
- double_account = double("Account")
20
+ book = double("book")
21
21
 
22
- You can also use the `mock` and `stub` methods to create test doubles, however
23
- these methods are there for backward compatibility only and will likely be
24
- deprecated and then removed from future versions.
22
+ Most of the time you will want some confidence that your doubles resemble an
23
+ existing object in your system. Verifying doubles are provided for this
24
+ purpose. If the existing object is available, they will prevent you from adding
25
+ stubs and expectations for methods that do not exist or that have an invalid
26
+ number of parameters.
27
+
28
+ book = instance_double("Book", :pages => 250)
29
+
30
+ Verifying doubles have some clever tricks to enable you to both test in
31
+ isolation without your dependencies loaded while still being able to validate
32
+ them against real objects.
25
33
 
26
34
  ## Method Stubs
27
35
 
28
36
  A method stub is an instruction to an object (real or test double) to return a
29
37
  known value in response to a message:
30
38
 
31
- die.stub(:roll) { 3 }
39
+ allow(die).to receive(:roll) { 3 }
32
40
 
33
41
  This tells the `die` object to return the value `3` when it receives the `roll`
34
42
  message.
@@ -45,7 +53,7 @@ specific message during the course of a code example:
45
53
  account = Account.new
46
54
  account.logger = logger
47
55
 
48
- logger.should_receive(:account_closed).with(account)
56
+ expect(logger).to receive(:account_closed).with(account)
49
57
 
50
58
  account.close
51
59
  end
@@ -4,10 +4,10 @@ Argument matchers can be used:
4
4
 
5
5
  * In stubs to constrain the scope of the stubbed method
6
6
 
7
- obj.stub(:foo).with(:bar) do |arg|
7
+ allow(obj).to receive(:foo).with(:bar) do |arg|
8
8
  #do something for :bar
9
9
  end
10
- obj.stub(:foo).with(:baz) do |arg|
10
+ allow(obj).to receive(:foo).with(:baz) do |arg|
11
11
  #do something for :baz
12
12
  end
13
13
 
@@ -17,11 +17,11 @@ Argument matchers can be used:
17
17
  obj = double()
18
18
 
19
19
  #expect a message with given args
20
- obj.should_receive(:message).with('an argument')
20
+ expect(obj).to receive(:message).with('an argument')
21
21
 
22
22
  If more control is needed, one can use a block
23
23
 
24
- obj.should_receive(:message) do |arg1, arg2|
24
+ expect(obj).to receive(:message) do |arg1, arg2|
25
25
  # set expectations about the args in this block
26
26
  # and optionally set a return value
27
- end
27
+ end
@@ -8,15 +8,15 @@ Feature: explicit arguments
8
8
  describe "stubbed explicit arguments" do
9
9
  it "works on stubs" do
10
10
  object = Object.new
11
- object.stub(:foo).with(:this) do |arg|
11
+ allow(object).to receive(:foo).with(:this) do |arg|
12
12
  "got this"
13
13
  end
14
- object.stub(:foo).with(:that) do |arg|
14
+ allow(object).to receive(:foo).with(:that) do |arg|
15
15
  "got that"
16
16
  end
17
17
 
18
- object.foo(:this).should eq("got this")
19
- object.foo(:that).should eq("got that")
18
+ expect(object.foo(:this)).to eq("got this")
19
+ expect(object.foo(:that)).to eq("got that")
20
20
  end
21
21
 
22
22
  it "works on doubles and expectations" do
@@ -36,10 +36,10 @@ Feature: explicit arguments
36
36
  describe "stubbed multiple explicit arguments" do
37
37
  it "works on stubs" do
38
38
  object = Object.new
39
- object.stub(:foo).with(:this) do |arg|
39
+ allow(object).to receive(:foo).with(:this) do |arg|
40
40
  "got this"
41
41
  end
42
- object.stub(:foo).with(:this, :that) do |arg1, arg2|
42
+ allow(object).to receive(:foo).with(:this, :that) do |arg1, arg2|
43
43
  "got this and that"
44
44
  end
45
45
 
@@ -11,7 +11,7 @@ Feature: General matchers
11
11
  describe "stubbed anything() args spec" do
12
12
  it "works" do
13
13
  object = Object.new
14
- object.stub(:foo).with(anything) do
14
+ allow(object).to receive(:foo).with(anything) do
15
15
  "anything"
16
16
  end
17
17
 
@@ -29,7 +29,7 @@ Feature: General matchers
29
29
  describe "stubbed any_args() args spec" do
30
30
  it "works" do
31
31
  object = Object.new
32
- object.stub(:foo).with(any_args) do
32
+ allow(object).to receive(:foo).with(any_args) do
33
33
  "anything"
34
34
  end
35
35
 
@@ -48,10 +48,10 @@ Feature: General matchers
48
48
  describe "stubbed no_args() args spec" do
49
49
  it "works for no args" do
50
50
  object = Object.new
51
- object.stub(:foo).with(no_args) do
51
+ allow(object).to receive(:foo).with(no_args) do
52
52
  "nothing"
53
53
  end
54
- object.stub(:foo).with(anything) do
54
+ allow(object).to receive(:foo).with(anything) do
55
55
  "something"
56
56
  end
57
57
 
@@ -10,10 +10,10 @@ Feature: stub with argument constraints
10
10
  describe "stubbed an_instance_of() args spec" do
11
11
  it "works" do
12
12
  object = Object.new
13
- object.stub(:foo).with(an_instance_of(Symbol)) do
13
+ allow(object).to receive(:foo).with(an_instance_of(Symbol)) do
14
14
  "symbol"
15
15
  end
16
- object.stub(:foo).with(an_instance_of(String)) do
16
+ allow(object).to receive(:foo).with(an_instance_of(String)) do
17
17
  "string"
18
18
  end
19
19
 
@@ -4,12 +4,14 @@
4
4
  obj = double()
5
5
 
6
6
  # expect a message
7
- obj.should_receive(:message)
7
+ expect(obj).to receive(:message)
8
8
 
9
9
  # specify a return value
10
- obj.should_receive(:message) { :value }
11
- obj.should_receive(:message => :value)
12
- obj.should_receive(:message).and_return(:value)
10
+ expect(obj).to receive(:message) { :value }
11
+ expect(obj).to receive(:message).and_return(:value)
12
+
13
+ # specify multiple message/return value pairs
14
+ expect(obj).to receive_messages(:message => :value, :another_message => :another_value)
13
15
 
14
16
  These forms are somewhat interchangeable. The difference is that the
15
17
  block contents are evaluated lazily when the `obj` receives the
@@ -17,57 +19,57 @@ block contents are evaluated lazily when the `obj` receives the
17
19
 
18
20
  ### Fake implementation
19
21
 
20
- obj.should_receive(:message) do |arg1, arg2|
22
+ expect(obj).to receive(:message) do |arg1, arg2|
21
23
  # set expectations about the args in this block
22
24
  # and set a return value
23
25
  end
24
26
 
25
27
  ### Using the original implementation
26
28
 
27
- obj.should_receive(:message).and_call_original
29
+ expect(obj).to receive(:message).and_call_original
28
30
 
29
31
  ### Raising/Throwing
30
32
 
31
- obj.should_receive(:message).and_raise("this error")
32
- obj.should_receive(:message).and_throw(:this_symbol)
33
+ expect(obj).to receive(:message).and_raise("this error")
34
+ expect(obj).to receive(:message).and_throw(:this_symbol)
33
35
 
34
36
  You can also use the block format:
35
37
 
36
- obj.should_receive(:message) { raise "this error" }
37
- obj.should_receive(:message) { throw :this_symbol }
38
+ expect(obj).to receive(:message) { raise "this error" }
39
+ expect(obj).to receive(:message) { throw :this_symbol }
38
40
 
39
41
  ### Argument constraints
40
42
 
41
43
  #### Explicit arguments
42
44
 
43
- obj.should_receive(:message).with('an argument')
44
- obj.should_receive(:message).with('more_than', 'one_argument')
45
+ expect(obj).to receive(:message).with('an argument')
46
+ expect(obj).to receive(:message).with('more_than', 'one_argument')
45
47
 
46
48
  #### Argument matchers
47
49
 
48
- obj.should_receive(:message).with(anything())
49
- obj.should_receive(:message).with(an_instance_of(Money))
50
- obj.should_receive(:message).with(hash_including(:a => 'b'))
50
+ expect(obj).to receive(:message).with(anything())
51
+ expect(obj).to receive(:message).with(an_instance_of(Money))
52
+ expect(obj).to receive(:message).with(hash_including(:a => 'b'))
51
53
 
52
54
  #### Regular expressions
53
55
 
54
- obj.should_receive(:message).with(/abc/)
56
+ expect(obj).to receive(:message).with(/abc/)
55
57
 
56
58
  ### Counts
57
59
 
58
- obj.should_receive(:message).once
59
- obj.should_receive(:message).twice
60
- obj.should_receive(:message).exactly(3).times
60
+ expect(obj).to receive(:message).once
61
+ expect(obj).to receive(:message).twice
62
+ expect(obj).to receive(:message).exactly(3).times
61
63
 
62
- obj.should_receive(:message).at_least(:once)
63
- obj.should_receive(:message).at_least(:twice)
64
- obj.should_receive(:message).at_least(n).times
64
+ expect(obj).to receive(:message).at_least(:once)
65
+ expect(obj).to receive(:message).at_least(:twice)
66
+ expect(obj).to receive(:message).at_least(n).times
65
67
 
66
- obj.should_receive(:message).at_most(:once)
67
- obj.should_receive(:message).at_most(:twice)
68
- obj.should_receive(:message).at_most(n).times
68
+ expect(obj).to receive(:message).at_most(:once)
69
+ expect(obj).to receive(:message).at_most(:twice)
70
+ expect(obj).to receive(:message).at_most(n).times
69
71
 
70
72
  ### Ordering
71
73
 
72
- obj.should_receive(:one).ordered
73
- obj.should_receive(:two).ordered
74
+ expect(obj).to receive(:one).ordered
75
+ expect(obj).to receive(:two).ordered