rspec-mocks 2.11.3 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/Changelog.md +16 -0
  2. data/README.md +26 -1
  3. data/features/argument_matchers/explicit.feature +2 -2
  4. data/features/argument_matchers/general_matchers.feature +4 -4
  5. data/features/argument_matchers/type_matchers.feature +1 -1
  6. data/features/message_expectations/README.md +4 -0
  7. data/features/message_expectations/any_instance.feature +1 -1
  8. data/features/message_expectations/call_original.feature +24 -0
  9. data/features/message_expectations/expect_message.feature +5 -5
  10. data/features/message_expectations/receive_counts.feature +7 -7
  11. data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +3 -3
  12. data/features/method_stubs/any_instance.feature +6 -7
  13. data/features/method_stubs/as_null_object.feature +1 -1
  14. data/features/method_stubs/simple_return_value.feature +2 -2
  15. data/features/method_stubs/stub_chain.feature +1 -1
  16. data/features/method_stubs/stub_implementation.feature +1 -1
  17. data/features/method_stubs/to_ary.feature +1 -1
  18. data/features/{stubbing_constants → mutating_constants}/README.md +21 -1
  19. data/features/mutating_constants/hiding_defined_constant.feature +64 -0
  20. data/features/{stubbing_constants → mutating_constants}/stub_defined_constant.feature +0 -0
  21. data/features/{stubbing_constants → mutating_constants}/stub_undefined_constant.feature +0 -0
  22. data/features/outside_rspec/configuration.feature +3 -3
  23. data/features/outside_rspec/standalone.feature +6 -5
  24. data/lib/rspec/mocks.rb +17 -1
  25. data/lib/rspec/mocks/any_instance.rb +12 -12
  26. data/lib/rspec/mocks/configuration.rb +28 -0
  27. data/lib/rspec/mocks/error_generator.rb +6 -0
  28. data/lib/rspec/mocks/example_methods.rb +34 -9
  29. data/lib/rspec/mocks/framework.rb +3 -2
  30. data/lib/rspec/mocks/instance_method_stasher.rb +70 -0
  31. data/lib/rspec/mocks/message_expectation.rb +49 -29
  32. data/lib/rspec/mocks/method_double.rb +84 -7
  33. data/lib/rspec/mocks/{stub_const.rb → mutate_const.rb} +117 -40
  34. data/lib/rspec/mocks/proxy.rb +16 -5
  35. data/lib/rspec/mocks/version.rb +1 -1
  36. data/spec/rspec/mocks/and_call_original_spec.rb +162 -0
  37. data/spec/rspec/mocks/any_instance_spec.rb +18 -7
  38. data/spec/rspec/mocks/configuration_spec.rb +26 -0
  39. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +9 -10
  40. data/spec/rspec/mocks/instance_method_stasher_spec.rb +58 -0
  41. data/spec/rspec/mocks/mock_spec.rb +35 -35
  42. data/spec/rspec/mocks/{stub_const_spec.rb → mutate_const_spec.rb} +142 -13
  43. data/spec/rspec/mocks/null_object_mock_spec.rb +3 -2
  44. data/spec/rspec/mocks/partial_mock_spec.rb +102 -77
  45. data/spec/rspec/mocks/serialization_spec.rb +1 -2
  46. data/spec/rspec/mocks/stub_implementation_spec.rb +6 -6
  47. data/spec/rspec/mocks_spec.rb +7 -0
  48. data/spec/spec_helper.rb +11 -0
  49. metadata +79 -80
  50. data/lib/rspec/mocks/stashed_instance_method.rb +0 -60
  51. data/spec/rspec/mocks/stashed_instance_method_spec.rb +0 -53
@@ -1,3 +1,19 @@
1
+ ### 2.12.0 / 2012-11-12
2
+ [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.11.3...2.12.0)
3
+
4
+ Enhancements
5
+
6
+ * `and_raise` can accept an exception class and message, more closely
7
+ matching `Kernel#raise` (e.g., `foo.stub(:bar).and_raise(RuntimeError, "message")`)
8
+ (Bas Vodde)
9
+ * Add `and_call_original`, which will delegate the message to the
10
+ original method (Myron Marston).
11
+
12
+ Deprecations:
13
+
14
+ * Add deprecation warning when using `and_return` with `should_not_receive`
15
+ (Neha Kumari)
16
+
1
17
  ### 2.11.3 / 2012-09-19
2
18
  [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.11.2...v2.11.3)
3
19
 
data/README.md CHANGED
@@ -38,7 +38,7 @@ method stub in one statement:
38
38
  book = double("book", :title => "The RSpec Book")
39
39
  ```
40
40
 
41
- The first argment is a name, which is used for documentation and appears in
41
+ The first argument is a name, which is used for documentation and appears in
42
42
  failure messages. If you don't care about the name, you can leave it out,
43
43
  making the combined instantiation/stub declaration very terse:
44
44
 
@@ -233,6 +233,31 @@ double.should_receive(:msg) do |arg|
233
233
  end
234
234
  ```
235
235
 
236
+ If the method being stubbed itself takes a block, and you need to yield to it
237
+ in some special way, you can use this:
238
+
239
+ ```ruby
240
+ double.should_receive(:msg) do |&arg|
241
+ begin
242
+ arg.call
243
+ ensure
244
+ # cleanup
245
+ end
246
+ end
247
+ ```
248
+
249
+ ## Delegating to the Original Implementation
250
+
251
+ When working with a partial mock object, you may occasionally
252
+ want to set a message expecation without interfering with how
253
+ the object responds to the message. You can use `and_call_original`
254
+ to achieve this:
255
+
256
+ ```ruby
257
+ Person.should_receive(:find).and_call_original
258
+ Person.find # => executes the original find method and returns the result
259
+ ```
260
+
236
261
  ## Combining Expectation Details
237
262
 
238
263
  Combining the message name with specific arguments, receive counts and responses
@@ -4,7 +4,7 @@ Feature: explicit arguments
4
4
 
5
5
  Scenario: explicit arguments
6
6
  Given a file named "stub_explicit_args_spec.rb" with:
7
- """
7
+ """ruby
8
8
  describe "stubbed explicit arguments" do
9
9
  it "works on stubs" do
10
10
  object = Object.new
@@ -32,7 +32,7 @@ Feature: explicit arguments
32
32
 
33
33
  Scenario: explicit arguments with multiple arities
34
34
  Given a file named "stub_multiple_explicit_args_spec.rb" with:
35
- """
35
+ """ruby
36
36
  describe "stubbed multiple explicit arguments" do
37
37
  it "works on stubs" do
38
38
  object = Object.new
@@ -7,7 +7,7 @@ Feature: General matchers
7
7
 
8
8
  Scenario: anything argument matcher
9
9
  Given a file named "stub_anything_args_spec.rb" with:
10
- """
10
+ """ruby
11
11
  describe "stubbed anything() args spec" do
12
12
  it "works" do
13
13
  object = Object.new
@@ -25,7 +25,7 @@ Feature: General matchers
25
25
 
26
26
  Scenario: any_args argument matcher
27
27
  Given a file named "stub_any_args_spec.rb" with:
28
- """
28
+ """ruby
29
29
  describe "stubbed any_args() args spec" do
30
30
  it "works" do
31
31
  object = Object.new
@@ -44,7 +44,7 @@ Feature: General matchers
44
44
 
45
45
  Scenario: no_args argument matcher
46
46
  Given a file named "stub_no_args_spec.rb" with:
47
- """
47
+ """ruby
48
48
  describe "stubbed no_args() args spec" do
49
49
  it "works for no args" do
50
50
  object = Object.new
@@ -65,7 +65,7 @@ Feature: General matchers
65
65
 
66
66
  Scenario: no_args argument matcher for expectations
67
67
  Given a file named "stub_no_args_expectations_spec.rb" with:
68
- """
68
+ """ruby
69
69
  describe "stubbed no_args() args spec for expectations" do
70
70
  it "works for no args" do
71
71
  object = Object.new
@@ -6,7 +6,7 @@ Feature: stub with argument constraints
6
6
 
7
7
  Scenario: an_instance_of argument matcher
8
8
  Given a file named "stub_an_instance_of_args_spec.rb" with:
9
- """
9
+ """ruby
10
10
  describe "stubbed an_instance_of() args spec" do
11
11
  it "works" do
12
12
  object = Object.new
@@ -22,6 +22,10 @@ block contents are evaluated lazily when the `obj` receives the
22
22
  # and set a return value
23
23
  end
24
24
 
25
+ ### Using the original implementation
26
+
27
+ obj.should_receive(:message).and_call_original
28
+
25
29
  ### Raising/Throwing
26
30
 
27
31
  obj.should_receive(:message).and_raise("this error")
@@ -7,7 +7,7 @@ Feature: expect a message on any instance of a class
7
7
 
8
8
  Scenario: expect a message on any instance of a class
9
9
  Given a file named "example_spec.rb" with:
10
- """
10
+ """ruby
11
11
  describe "any_instance.should_receive" do
12
12
  it "verifies that one instance of the class receives the message" do
13
13
  Object.any_instance.should_receive(:foo).and_return(:return_value)
@@ -0,0 +1,24 @@
1
+ Feature: Calling the original method
2
+
3
+ You can use `and_call_original` on the fluent interface
4
+ to "pass through" the received message to the original method.
5
+
6
+ Scenario: expect a message
7
+ Given a file named "call_original_spec.rb" with:
8
+ """ruby
9
+ class Addition
10
+ def self.two_plus_two
11
+ 4
12
+ end
13
+ end
14
+
15
+ describe "and_call_original" do
16
+ it "delegates the message to the original implementation" do
17
+ Addition.should_receive(:two_plus_two).and_call_original
18
+ Addition.two_plus_two.should eq(4)
19
+ end
20
+ end
21
+ """
22
+ When I run `rspec call_original_spec.rb`
23
+ Then the examples should all pass
24
+
@@ -5,7 +5,7 @@ Feature: expect a message
5
5
 
6
6
  Scenario: expect a message
7
7
  Given a file named "spec/account_spec.rb" with:
8
- """
8
+ """ruby
9
9
  require "account"
10
10
 
11
11
  describe Account do
@@ -23,7 +23,7 @@ Feature: expect a message
23
23
  end
24
24
  """
25
25
  And a file named "lib/account.rb" with:
26
- """
26
+ """ruby
27
27
  class Account
28
28
  attr_accessor :logger
29
29
 
@@ -37,7 +37,7 @@ Feature: expect a message
37
37
 
38
38
  Scenario: expect a message with an argument
39
39
  Given a file named "spec/account_spec.rb" with:
40
- """
40
+ """ruby
41
41
  require "account"
42
42
 
43
43
  describe Account do
@@ -55,7 +55,7 @@ Feature: expect a message
55
55
  end
56
56
  """
57
57
  And a file named "lib/account.rb" with:
58
- """
58
+ """ruby
59
59
  class Account
60
60
  attr_accessor :logger
61
61
 
@@ -69,7 +69,7 @@ Feature: expect a message
69
69
 
70
70
  Scenario: provide a return value
71
71
  Given a file named "message_expectation_spec.rb" with:
72
- """
72
+ """ruby
73
73
  describe "a message expectation" do
74
74
  context "with a return value" do
75
75
  context "specified in a block" do
@@ -2,7 +2,7 @@ Feature: receive counts
2
2
 
3
3
  Scenario: expect a message once
4
4
  Given a file named "spec/account_spec.rb" with:
5
- """
5
+ """ruby
6
6
  class Account
7
7
  attr_accessor :logger
8
8
 
@@ -30,7 +30,7 @@ Feature: receive counts
30
30
 
31
31
  Scenario: expect a message twice
32
32
  Given a file named "spec/account_spec.rb" with:
33
- """
33
+ """ruby
34
34
  class Account
35
35
  attr_accessor :logger
36
36
 
@@ -59,7 +59,7 @@ Feature: receive counts
59
59
 
60
60
  Scenario: expect a message 3 times
61
61
  Given a file named "spec/account_spec.rb" with:
62
- """
62
+ """ruby
63
63
  class Account
64
64
  attr_accessor :logger
65
65
 
@@ -89,7 +89,7 @@ Feature: receive counts
89
89
 
90
90
  Scenario: expect a message at least (:once)
91
91
  Given a file named "spec/account_spec.rb" with:
92
- """
92
+ """ruby
93
93
  class Account
94
94
  attr_accessor :logger
95
95
 
@@ -118,7 +118,7 @@ Feature: receive counts
118
118
 
119
119
  Scenario: expect a message at least (n) times
120
120
  Given a file named "spec/account_spec.rb" with:
121
- """
121
+ """ruby
122
122
  class Account
123
123
  attr_accessor :logger
124
124
 
@@ -151,7 +151,7 @@ Feature: receive counts
151
151
 
152
152
  Scenario: expect a message at most (:once)
153
153
  Given a file named "spec/account_spec.rb" with:
154
- """
154
+ """ruby
155
155
  class Account
156
156
  attr_accessor :logger
157
157
 
@@ -181,7 +181,7 @@ Feature: receive counts
181
181
 
182
182
  Scenario: expect a message at most (n) times
183
183
  Given a file named "spec/account_spec.rb" with:
184
- """
184
+ """ruby
185
185
  class Account
186
186
  attr_accessor :logger
187
187
 
@@ -2,7 +2,7 @@ Feature: warn when expectation is set on nil
2
2
 
3
3
  Scenario: nil instance variable
4
4
  Given a file named "example_spec.rb" with:
5
- """
5
+ """ruby
6
6
  RSpec.configure {|c| c.mock_with :rspec}
7
7
  describe "something" do
8
8
  it "does something" do
@@ -16,7 +16,7 @@ Feature: warn when expectation is set on nil
16
16
 
17
17
  Scenario: allow
18
18
  Given a file named "example_spec.rb" with:
19
- """
19
+ """ruby
20
20
  RSpec.configure {|c| c.mock_with :rspec}
21
21
  describe "something" do
22
22
  it "does something" do
@@ -31,7 +31,7 @@ Feature: warn when expectation is set on nil
31
31
 
32
32
  Scenario: allow in one example, but not on another
33
33
  Given a file named "example_spec.rb" with:
34
- """
34
+ """ruby
35
35
  RSpec.configure {|c| c.mock_with :rspec}
36
36
  describe "something" do
37
37
  it "does something (foo)" do
@@ -8,7 +8,7 @@ Feature: stub on any instance of a class
8
8
 
9
9
  Scenario: any_instance stub with a single return value
10
10
  Given a file named "example_spec.rb" with:
11
- """
11
+ """ruby
12
12
  describe "any_instance.stub" do
13
13
  it "returns the specified value on any instance of the class" do
14
14
  Object.any_instance.stub(:foo).and_return(:return_value)
@@ -23,7 +23,7 @@ Feature: stub on any instance of a class
23
23
 
24
24
  Scenario: any_instance stub with a hash
25
25
  Given a file named "example_spec.rb" with:
26
- """
26
+ """ruby
27
27
  describe "any_instance.stub" do
28
28
  context "with a hash" do
29
29
  it "returns the hash values on any instance of the class" do
@@ -41,7 +41,7 @@ Feature: stub on any instance of a class
41
41
 
42
42
  Scenario: any_instance stub with specific arguments matchers
43
43
  Given a file named "example_spec.rb" with:
44
- """
44
+ """ruby
45
45
  describe "any_instance.stub" do
46
46
  context "with arguments" do
47
47
  it "returns the stubbed value when arguments match" do
@@ -60,7 +60,7 @@ Feature: stub on any instance of a class
60
60
 
61
61
  Scenario: any_instance unstub
62
62
  Given a file named "example_spec.rb" with:
63
- """
63
+ """ruby
64
64
  describe "any_instance.unstub" do
65
65
  it "unstubs a stubbed method" do
66
66
  class Object
@@ -81,7 +81,7 @@ Feature: stub on any instance of a class
81
81
 
82
82
  Scenario: any_instance unstub
83
83
  Given a file named "example_spec.rb" with:
84
- """
84
+ """ruby
85
85
  describe "any_instance.unstub" do
86
86
  context "with both an expectation and a stub already set" do
87
87
  it "only removes the stub" do
@@ -104,7 +104,7 @@ Feature: stub on any instance of a class
104
104
 
105
105
  Scenario: stub a chain of methods an any instance
106
106
  Given a file named "stub_chain_spec.rb" with:
107
- """
107
+ """ruby
108
108
  describe "stubbing a chain of methods" do
109
109
  context "given symbols representing methods" do
110
110
  it "returns the correct value" do
@@ -130,4 +130,3 @@ Feature: stub on any instance of a class
130
130
  """
131
131
  When I run `rspec stub_chain_spec.rb`
132
132
  Then the examples should all pass
133
-
@@ -8,7 +8,7 @@ Feature: as_null_object
8
8
 
9
9
  Scenario: double acting as_null_object
10
10
  Given a file named "as_null_object_spec.rb" with:
11
- """
11
+ """ruby
12
12
  describe "a double with as_null_object called" do
13
13
  let(:null_object) { double('null object').as_null_object }
14
14
 
@@ -6,7 +6,7 @@ Feature: stub with a simple return value
6
6
 
7
7
  Scenario: stub with no return value
8
8
  Given a file named "example_spec.rb" with:
9
- """
9
+ """ruby
10
10
  describe "a stub with no return value specified" do
11
11
  let(:collaborator) { double("collaborator") }
12
12
 
@@ -21,7 +21,7 @@ Feature: stub with a simple return value
21
21
 
22
22
  Scenario: stubs with return values
23
23
  Given a file named "example_spec.rb" with:
24
- """
24
+ """ruby
25
25
  describe "a stub with a return value" do
26
26
  context "specified in a block" do
27
27
  it "returns the specified value" do
@@ -21,7 +21,7 @@ Feature: stub a chain of methods
21
21
 
22
22
  Scenario: stub a chain of methods
23
23
  Given a file named "stub_chain_spec.rb" with:
24
- """
24
+ """ruby
25
25
  describe "stubbing a chain of methods" do
26
26
  subject { Object.new }
27
27
 
@@ -5,7 +5,7 @@ Feature: stub with substitute implementation
5
5
 
6
6
  Scenario: stub implementation
7
7
  Given a file named "stub_implementation_spec.rb" with:
8
- """
8
+ """ruby
9
9
  describe "a stubbed implementation" do
10
10
  it "works" do
11
11
  object = Object.new
@@ -14,7 +14,7 @@ Feature: double handling to_ary
14
14
 
15
15
  Scenario: double receiving to_ary
16
16
  Given a file named "example.rb" with:
17
- """
17
+ """ruby
18
18
  describe "#to_ary" do
19
19
  shared_examples "to_ary" do
20
20
  it "raises a NoMethodError" do
@@ -1,4 +1,6 @@
1
- ## Stubbing Constants
1
+ ## Mutating Constants
2
+
3
+ ### Stubbing
2
4
 
3
5
  Support is provided for stubbing constants. Like with method stubs, the
4
6
  stubbed constants will be restored to their original state when an
@@ -60,3 +62,21 @@ CardDeck::SUITS # => [:Spades, :Diamonds, :Clubs, :Hearts]
60
62
  CardDeck::NUM_CARDS # => raises uninitialized constant error
61
63
  ```
62
64
 
65
+ ### Hiding
66
+
67
+ Support is also provided for hiding constants. Hiding a constant temporarily
68
+ removes it; it is restored to its original value after the test completes.
69
+
70
+ ```ruby
71
+ FOO = 42
72
+ hide_const("FOO")
73
+ FOO => NameError: uninitialized constant FOO
74
+ ```
75
+
76
+ Like stubbed constants, names must be fully qualified.
77
+
78
+ Hiding constants that are already undefined has no effect.
79
+
80
+ ```ruby
81
+ hide_const("NO_OP")
82
+ ```