rspec-mocks 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Changelog.md +104 -36
- data/README.md +35 -4
- data/lib/rspec/mocks.rb +52 -13
- data/lib/rspec/mocks/any_instance.rb +10 -0
- data/lib/rspec/mocks/any_instance/chain.rb +17 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +1 -1
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +9 -6
- data/lib/rspec/mocks/any_instance/message_chains.rb +10 -1
- data/lib/rspec/mocks/any_instance/proxy.rb +112 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +49 -18
- data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -3
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +1 -1
- data/lib/rspec/mocks/argument_list_matcher.rb +5 -3
- data/lib/rspec/mocks/argument_matchers.rb +28 -12
- data/lib/rspec/mocks/configuration.rb +29 -0
- data/lib/rspec/mocks/error_generator.rb +2 -5
- data/lib/rspec/mocks/example_methods.rb +122 -13
- data/lib/rspec/mocks/instance_method_stasher.rb +26 -2
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/receive.rb +6 -20
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +7 -5
- data/lib/rspec/mocks/matchers/receive_messages.rb +1 -2
- data/lib/rspec/mocks/message_chain.rb +9 -9
- data/lib/rspec/mocks/message_expectation.rb +6 -5
- data/lib/rspec/mocks/method_double.rb +67 -10
- data/lib/rspec/mocks/method_reference.rb +1 -1
- data/lib/rspec/mocks/mutate_const.rb +10 -14
- data/lib/rspec/mocks/object_reference.rb +7 -4
- data/lib/rspec/mocks/proxy.rb +95 -8
- data/lib/rspec/mocks/space.rb +58 -9
- data/lib/rspec/mocks/syntax.rb +128 -210
- data/lib/rspec/mocks/targets.rb +6 -8
- data/lib/rspec/mocks/test_double.rb +16 -25
- data/lib/rspec/mocks/verifying_double.rb +15 -12
- data/lib/rspec/mocks/verifying_message_expecation.rb +3 -3
- data/lib/rspec/mocks/verifying_proxy.rb +23 -17
- data/lib/rspec/mocks/version.rb +3 -1
- metadata +29 -257
- metadata.gz.sig +0 -0
- data/features/README.md +0 -75
- data/features/Scope.md +0 -17
- data/features/Upgrade.md +0 -22
- data/features/argument_matchers/README.md +0 -27
- data/features/argument_matchers/explicit.feature +0 -59
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -26
- data/features/message_expectations/README.md +0 -75
- data/features/message_expectations/allow_any_instance_of.feature +0 -26
- data/features/message_expectations/any_instance.feature +0 -43
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/call_original.feature +0 -23
- data/features/message_expectations/expect_message_using_expect.feature +0 -107
- data/features/message_expectations/expect_message_using_should_receive.feature +0 -118
- data/features/message_expectations/message_chains_using_expect.feature +0 -49
- data/features/message_expectations/receive_counts.feature +0 -209
- data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
- data/features/method_stubs/README.md +0 -77
- data/features/method_stubs/allow_any_instance_of.feature +0 -136
- data/features/method_stubs/as_null_object.feature +0 -40
- data/features/method_stubs/receive_message_chain.feature +0 -51
- data/features/method_stubs/simple_return_value_with_allow.feature +0 -44
- data/features/method_stubs/simple_return_value_with_stub.feature +0 -64
- data/features/method_stubs/stub_implementation.feature +0 -48
- data/features/method_stubs/to_ary.feature +0 -51
- data/features/mutating_constants/README.md +0 -82
- data/features/mutating_constants/hiding_defined_constant.feature +0 -64
- data/features/mutating_constants/stub_defined_constant.feature +0 -78
- data/features/mutating_constants/stub_undefined_constant.feature +0 -50
- data/features/outside_rspec/standalone.feature +0 -33
- data/features/spies/spy_partial_mock_method.feature +0 -34
- data/features/spies/spy_pure_mock_method.feature +0 -76
- data/features/spies/spy_unstubbed_method.feature +0 -18
- data/features/step_definitions/additional_cli_steps.rb +0 -11
- data/features/support/env.rb +0 -22
- data/features/support/rubinius.rb +0 -6
- data/features/test_frameworks/test_unit.feature +0 -54
- data/features/verifying_doubles/README.md +0 -17
- data/features/verifying_doubles/class_doubles.feature +0 -73
- data/features/verifying_doubles/dynamic_classes.feature +0 -71
- data/features/verifying_doubles/instance_doubles.feature +0 -103
- data/features/verifying_doubles/object_doubles.feature +0 -65
- data/features/verifying_doubles/partial_doubles.feature +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -17
- data/lib/rspec/mocks/framework.rb +0 -35
- data/lib/rspec/mocks/method_signature_verifier.rb +0 -207
- data/lib/rspec/mocks/ruby_features.rb +0 -24
- data/spec/rspec/mocks/and_call_original_spec.rb +0 -268
- data/spec/rspec/mocks/and_return_spec.rb +0 -23
- data/spec/rspec/mocks/and_yield_spec.rb +0 -126
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -41
- data/spec/rspec/mocks/any_instance_spec.rb +0 -1045
- data/spec/rspec/mocks/array_including_matcher_spec.rb +0 -41
- data/spec/rspec/mocks/at_least_spec.rb +0 -123
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/before_all_spec.rb +0 -132
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -70
- data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +0 -204
- data/spec/rspec/mocks/configuration_spec.rb +0 -242
- data/spec/rspec/mocks/double_spec.rb +0 -874
- data/spec/rspec/mocks/example_methods_spec.rb +0 -13
- data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -54
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -184
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -94
- data/spec/rspec/mocks/instance_method_stasher_spec.rb +0 -74
- data/spec/rspec/mocks/matchers/have_received_spec.rb +0 -347
- data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +0 -198
- data/spec/rspec/mocks/matchers/receive_messages_spec.rb +0 -140
- data/spec/rspec/mocks/matchers/receive_spec.rb +0 -418
- data/spec/rspec/mocks/method_signature_verifier_spec.rb +0 -272
- data/spec/rspec/mocks/methods_spec.rb +0 -26
- data/spec/rspec/mocks/mock_expectation_error_spec.rb +0 -22
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -114
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -132
- data/spec/rspec/mocks/mutate_const_spec.rb +0 -542
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -52
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -133
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/order_group_spec.rb +0 -27
- data/spec/rspec/mocks/partial_double_spec.rb +0 -308
- data/spec/rspec/mocks/partial_double_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -145
- data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
- data/spec/rspec/mocks/record_messages_spec.rb +0 -26
- data/spec/rspec/mocks/serialization_spec.rb +0 -90
- data/spec/rspec/mocks/space_spec.rb +0 -244
- data/spec/rspec/mocks/stash_spec.rb +0 -46
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -168
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -327
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -58
- data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +0 -101
- data/spec/rspec/mocks/syntax_spec.rb +0 -19
- data/spec/rspec/mocks/test_double_spec.rb +0 -49
- data/spec/rspec/mocks/to_ary_spec.rb +0 -54
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks/verifying_double_spec.rb +0 -590
- data/spec/rspec/mocks/verifying_message_expecation_spec.rb +0 -69
- data/spec/rspec/mocks_spec.rb +0 -192
- data/spec/spec_helper.rb +0 -121
metadata.gz.sig
CHANGED
Binary file
|
data/features/README.md
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
rspec-mocks helps to control the context in a code example by letting you set
|
2
|
-
known return values, fake implementations of methods, and even expectations
|
3
|
-
that specific messages are received by an object.
|
4
|
-
|
5
|
-
You can do these three things on test doubles that rspec-mocks creates for you
|
6
|
-
on the fly, or you can do them on objects that are part of your system.
|
7
|
-
|
8
|
-
## Messages and Methods
|
9
|
-
|
10
|
-
_Message_ and _method_ are metaphors that we use somewhat interchangeably, but
|
11
|
-
they are subtly different. In Object Oriented Programming, objects communicate
|
12
|
-
by sending _messages_ to one another. When an object receives a message, it
|
13
|
-
invokes a _method_ with the same name as the message.
|
14
|
-
|
15
|
-
## Test Doubles
|
16
|
-
|
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, passing in an optional identifier, to create one:
|
19
|
-
|
20
|
-
book = double("book")
|
21
|
-
|
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.
|
33
|
-
|
34
|
-
## Method Stubs
|
35
|
-
|
36
|
-
A method stub is an instruction to an object (real or test double) to return a
|
37
|
-
known value in response to a message:
|
38
|
-
|
39
|
-
allow(die).to receive(:roll) { 3 }
|
40
|
-
|
41
|
-
This tells the `die` object to return the value `3` when it receives the `roll`
|
42
|
-
message.
|
43
|
-
|
44
|
-
## Message Expectations
|
45
|
-
|
46
|
-
A message expectation is an expectation that an object should receive a
|
47
|
-
specific message during the course of a code example:
|
48
|
-
|
49
|
-
describe Account do
|
50
|
-
context "when closed" do
|
51
|
-
it "logs an 'account closed' message" do
|
52
|
-
logger = double()
|
53
|
-
account = Account.new
|
54
|
-
account.logger = logger
|
55
|
-
|
56
|
-
expect(logger).to receive(:account_closed).with(account)
|
57
|
-
|
58
|
-
account.close
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
This example specifies that the `account` object sends the `logger` the
|
64
|
-
`account_closed` message (with itself as an argument) when it receives the
|
65
|
-
`close` message.
|
66
|
-
|
67
|
-
## Issues
|
68
|
-
|
69
|
-
The documentation for rspec-mocks is a work in progress. We'll be adding
|
70
|
-
Cucumber features over time, and clarifying existing ones. If you have
|
71
|
-
specific features you'd like to see added, find the existing documentation
|
72
|
-
incomplete or confusing, or, better yet, wish to write a missing Cucumber
|
73
|
-
feature yourself, please [submit an
|
74
|
-
issue](http://github.com/rspec/rspec-mocks/issues) or a [pull
|
75
|
-
request](http://github.com/rspec/rspec-mocks).
|
data/features/Scope.md
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
Doubles, stubs, and message expectations are all cleaned out after each
|
2
|
-
example. This ensures that each example can be run in isolation, and in any
|
3
|
-
order.
|
4
|
-
|
5
|
-
### `before(:each)`
|
6
|
-
|
7
|
-
It is perfectly fine to set up doubles, stubs, and message expectations in
|
8
|
-
a `before(:each)` hook, as that hook is executed in the scope of the example:
|
9
|
-
|
10
|
-
before(:each) do
|
11
|
-
@account = double('account')
|
12
|
-
end
|
13
|
-
|
14
|
-
### Do not create doubles, stubs, or message expectations in `before(:all)`
|
15
|
-
|
16
|
-
If you do, they'll get cleaned out after the first example, and you will be
|
17
|
-
very confused as to what's going on in the second example.
|
data/features/Upgrade.md
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
## rspec-mocks-2.6
|
2
|
-
|
3
|
-
### `any_instance`
|
4
|
-
|
5
|
-
Set method stubs and message expectations on any instance of a class:
|
6
|
-
|
7
|
-
class Foo; end
|
8
|
-
Foo.any_instance.stub(:bar) { 'bar' }
|
9
|
-
Foo.new.bar # => 'bar'
|
10
|
-
|
11
|
-
## rspec-mocks-2.2
|
12
|
-
|
13
|
-
### `require "rspec/mocks/standalone"`
|
14
|
-
|
15
|
-
Sets up top-level environment to explore rspec-mocks. Mostly useful in irb:
|
16
|
-
|
17
|
-
$ irb
|
18
|
-
> require 'rspec/mocks/standalone'
|
19
|
-
> foo = double()
|
20
|
-
> foo.stub(:bar) { :baz }
|
21
|
-
> foo.bar
|
22
|
-
=> :baz
|
@@ -1,27 +0,0 @@
|
|
1
|
-
### Introduction
|
2
|
-
|
3
|
-
Argument matchers can be used:
|
4
|
-
|
5
|
-
* In stubs to constrain the scope of the stubbed method
|
6
|
-
|
7
|
-
allow(obj).to receive(:foo).with(:bar) do |arg|
|
8
|
-
#do something for :bar
|
9
|
-
end
|
10
|
-
allow(obj).to receive(:foo).with(:baz) do |arg|
|
11
|
-
#do something for :baz
|
12
|
-
end
|
13
|
-
|
14
|
-
* In expectations to validate the arguments that should be received in a method call
|
15
|
-
|
16
|
-
#create a double
|
17
|
-
obj = double()
|
18
|
-
|
19
|
-
#expect a message with given args
|
20
|
-
expect(obj).to receive(:message).with('an argument')
|
21
|
-
|
22
|
-
If more control is needed, one can use a block
|
23
|
-
|
24
|
-
expect(obj).to receive(:message) do |arg1, arg2|
|
25
|
-
# set expectations about the args in this block
|
26
|
-
# and optionally set a return value
|
27
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
Feature: explicit arguments
|
2
|
-
|
3
|
-
Allows you to explicitly specify the argument values
|
4
|
-
|
5
|
-
Scenario: explicit arguments
|
6
|
-
Given a file named "stub_explicit_args_spec.rb" with:
|
7
|
-
"""ruby
|
8
|
-
describe "stubbed explicit arguments" do
|
9
|
-
it "works on stubs" do
|
10
|
-
object = Object.new
|
11
|
-
allow(object).to receive(:foo).with(:this) do |arg|
|
12
|
-
"got this"
|
13
|
-
end
|
14
|
-
allow(object).to receive(:foo).with(:that) do |arg|
|
15
|
-
"got that"
|
16
|
-
end
|
17
|
-
|
18
|
-
expect(object.foo(:this)).to eq("got this")
|
19
|
-
expect(object.foo(:that)).to eq("got that")
|
20
|
-
end
|
21
|
-
|
22
|
-
it "works on doubles and expectations" do
|
23
|
-
object = double('foo')
|
24
|
-
expect(object).to receive(:bar).with(:foo)
|
25
|
-
|
26
|
-
object.bar(:foo)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
"""
|
30
|
-
When I run `rspec stub_explicit_args_spec.rb`
|
31
|
-
Then the output should contain "2 examples, 0 failures"
|
32
|
-
|
33
|
-
Scenario: explicit arguments with multiple arities
|
34
|
-
Given a file named "stub_multiple_explicit_args_spec.rb" with:
|
35
|
-
"""ruby
|
36
|
-
describe "stubbed multiple explicit arguments" do
|
37
|
-
it "works on stubs" do
|
38
|
-
object = Object.new
|
39
|
-
allow(object).to receive(:foo).with(:this) do |arg|
|
40
|
-
"got this"
|
41
|
-
end
|
42
|
-
allow(object).to receive(:foo).with(:this, :that) do |arg1, arg2|
|
43
|
-
"got this and that"
|
44
|
-
end
|
45
|
-
|
46
|
-
expect(object.foo(:this)).to eq("got this")
|
47
|
-
expect(object.foo(:this, :that)).to eq("got this and that")
|
48
|
-
end
|
49
|
-
|
50
|
-
it "works on mocks" do
|
51
|
-
object = double('foo')
|
52
|
-
expect(object).to receive(:foo).with(:this, :that)
|
53
|
-
|
54
|
-
object.foo(:this, :that)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
"""
|
58
|
-
When I run `rspec stub_multiple_explicit_args_spec.rb`
|
59
|
-
Then the output should contain "2 examples, 0 failures"
|
@@ -1,85 +0,0 @@
|
|
1
|
-
Feature: General matchers
|
2
|
-
|
3
|
-
The `anything`, `any_args`, and `no_args` matchers can be used to require the method
|
4
|
-
to have arguments (or not) without constraining the details of the argument, such as its
|
5
|
-
type, pattern or value. The `anything` matcher only reflects a single argument, while
|
6
|
-
the `any_args` matcher matches any arity.
|
7
|
-
|
8
|
-
Scenario: anything argument matcher
|
9
|
-
Given a file named "stub_anything_args_spec.rb" with:
|
10
|
-
"""ruby
|
11
|
-
describe "stubbed anything() args spec" do
|
12
|
-
it "works" do
|
13
|
-
object = Object.new
|
14
|
-
allow(object).to receive(:foo).with(anything) do
|
15
|
-
"anything"
|
16
|
-
end
|
17
|
-
|
18
|
-
expect(object.foo(1)).to eq("anything")
|
19
|
-
expect(object.foo(:that)).to eq("anything")
|
20
|
-
end
|
21
|
-
end
|
22
|
-
"""
|
23
|
-
When I run `rspec stub_anything_args_spec.rb`
|
24
|
-
Then the output should contain "1 example, 0 failures"
|
25
|
-
|
26
|
-
Scenario: any_args argument matcher
|
27
|
-
Given a file named "stub_any_args_spec.rb" with:
|
28
|
-
"""ruby
|
29
|
-
describe "stubbed any_args() args spec" do
|
30
|
-
it "works" do
|
31
|
-
object = Object.new
|
32
|
-
allow(object).to receive(:foo).with(any_args) do
|
33
|
-
"anything"
|
34
|
-
end
|
35
|
-
|
36
|
-
expect(object.foo(1)).to eq("anything")
|
37
|
-
expect(object.foo(:that)).to eq("anything")
|
38
|
-
expect(object.foo).to eq("anything")
|
39
|
-
end
|
40
|
-
end
|
41
|
-
"""
|
42
|
-
When I run `rspec stub_any_args_spec.rb`
|
43
|
-
Then the output should contain "1 example, 0 failures"
|
44
|
-
|
45
|
-
Scenario: no_args argument matcher
|
46
|
-
Given a file named "stub_no_args_spec.rb" with:
|
47
|
-
"""ruby
|
48
|
-
describe "stubbed no_args() args spec" do
|
49
|
-
it "works for no args" do
|
50
|
-
object = Object.new
|
51
|
-
allow(object).to receive(:foo).with(no_args) do
|
52
|
-
"nothing"
|
53
|
-
end
|
54
|
-
allow(object).to receive(:foo).with(anything) do
|
55
|
-
"something"
|
56
|
-
end
|
57
|
-
|
58
|
-
expect(object.foo(:that)).to eq("something")
|
59
|
-
expect(object.foo).to eq("nothing")
|
60
|
-
end
|
61
|
-
end
|
62
|
-
"""
|
63
|
-
When I run `rspec stub_no_args_spec.rb`
|
64
|
-
Then the output should contain "1 example, 0 failures"
|
65
|
-
|
66
|
-
Scenario: no_args argument matcher for expectations
|
67
|
-
Given a file named "stub_no_args_expectations_spec.rb" with:
|
68
|
-
"""ruby
|
69
|
-
describe "stubbed no_args() args spec for expectations" do
|
70
|
-
it "works for no args" do
|
71
|
-
object = Object.new
|
72
|
-
expect(object).to receive(:foo).with(no_args)
|
73
|
-
|
74
|
-
object.foo
|
75
|
-
end
|
76
|
-
it "fails for args" do
|
77
|
-
object = Object.new
|
78
|
-
expect(object).to receive(:foo).with(no_args)
|
79
|
-
|
80
|
-
object.foo(:bar)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
"""
|
84
|
-
When I run `rspec stub_no_args_expectations_spec.rb`
|
85
|
-
Then the output should contain "2 examples, 1 failure"
|
@@ -1,26 +0,0 @@
|
|
1
|
-
Feature: stub with argument constraints
|
2
|
-
|
3
|
-
You can further specify the behavior by constraining the type,
|
4
|
-
format and/or number of arguments with the `#with()` method
|
5
|
-
chained off of `#stub()`
|
6
|
-
|
7
|
-
Scenario: an_instance_of argument matcher
|
8
|
-
Given a file named "stub_an_instance_of_args_spec.rb" with:
|
9
|
-
"""ruby
|
10
|
-
describe "stubbed an_instance_of() args spec" do
|
11
|
-
it "works" do
|
12
|
-
object = Object.new
|
13
|
-
allow(object).to receive(:foo).with(an_instance_of(Symbol)) do
|
14
|
-
"symbol"
|
15
|
-
end
|
16
|
-
allow(object).to receive(:foo).with(an_instance_of(String)) do
|
17
|
-
"string"
|
18
|
-
end
|
19
|
-
|
20
|
-
expect(object.foo("bar")).to eq("string")
|
21
|
-
expect(object.foo(:that)).to eq("symbol")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
"""
|
25
|
-
When I run `rspec stub_an_instance_of_args_spec.rb`
|
26
|
-
Then the output should contain "1 example, 0 failures"
|
@@ -1,75 +0,0 @@
|
|
1
|
-
### Basics
|
2
|
-
|
3
|
-
# create a double
|
4
|
-
obj = double()
|
5
|
-
|
6
|
-
# expect a message
|
7
|
-
expect(obj).to receive(:message)
|
8
|
-
|
9
|
-
# specify a 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)
|
15
|
-
|
16
|
-
These forms are somewhat interchangeable. The difference is that the
|
17
|
-
block contents are evaluated lazily when the `obj` receives the
|
18
|
-
`message` message, whereas the others are evaluated as they are read.
|
19
|
-
|
20
|
-
### Fake implementation
|
21
|
-
|
22
|
-
expect(obj).to receive(:message) do |arg1, arg2|
|
23
|
-
# set expectations about the args in this block
|
24
|
-
# and set a return value
|
25
|
-
end
|
26
|
-
|
27
|
-
### Using the original implementation
|
28
|
-
|
29
|
-
expect(obj).to receive(:message).and_call_original
|
30
|
-
|
31
|
-
### Raising/Throwing
|
32
|
-
|
33
|
-
expect(obj).to receive(:message).and_raise("this error")
|
34
|
-
expect(obj).to receive(:message).and_throw(:this_symbol)
|
35
|
-
|
36
|
-
You can also use the block format:
|
37
|
-
|
38
|
-
expect(obj).to receive(:message) { raise "this error" }
|
39
|
-
expect(obj).to receive(:message) { throw :this_symbol }
|
40
|
-
|
41
|
-
### Argument constraints
|
42
|
-
|
43
|
-
#### Explicit arguments
|
44
|
-
|
45
|
-
expect(obj).to receive(:message).with('an argument')
|
46
|
-
expect(obj).to receive(:message).with('more_than', 'one_argument')
|
47
|
-
|
48
|
-
#### Argument matchers
|
49
|
-
|
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'))
|
53
|
-
|
54
|
-
#### Regular expressions
|
55
|
-
|
56
|
-
expect(obj).to receive(:message).with(/abc/)
|
57
|
-
|
58
|
-
### Counts
|
59
|
-
|
60
|
-
expect(obj).to receive(:message).once
|
61
|
-
expect(obj).to receive(:message).twice
|
62
|
-
expect(obj).to receive(:message).exactly(3).times
|
63
|
-
|
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
|
67
|
-
|
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
|
71
|
-
|
72
|
-
### Ordering
|
73
|
-
|
74
|
-
expect(obj).to receive(:one).ordered
|
75
|
-
expect(obj).to receive(:two).ordered
|
@@ -1,26 +0,0 @@
|
|
1
|
-
Feature: allow a message on any instance of a class
|
2
|
-
|
3
|
-
Use `allow_any_instance_of(Class).to receive` when you want to configure how
|
4
|
-
instances of the given class respond to a message without setting an
|
5
|
-
expectation that the message will be received.
|
6
|
-
|
7
|
-
Scenario: allowing a message on any instance of a class
|
8
|
-
Given a file named "example_spec.rb" with:
|
9
|
-
"""ruby
|
10
|
-
describe "any_instance.to receive" do
|
11
|
-
before do
|
12
|
-
allow_any_instance_of(Object).to receive(:foo).and_return(:return_value)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "allows any instance of the class to receive the message" do
|
16
|
-
o = Object.new
|
17
|
-
expect(o.foo).to eq(:return_value)
|
18
|
-
end
|
19
|
-
|
20
|
-
it "passes even if no instances receive that message" do
|
21
|
-
o = Object.new
|
22
|
-
end
|
23
|
-
end
|
24
|
-
"""
|
25
|
-
When I run `rspec example_spec.rb`
|
26
|
-
Then the examples should all pass
|
@@ -1,43 +0,0 @@
|
|
1
|
-
Feature: expect a message on any instance of a class
|
2
|
-
|
3
|
-
Use `expect_any_instance_of(Class).to receive` to set an expectation that one
|
4
|
-
(and only one) instance of a class receives a message before the example is
|
5
|
-
completed.
|
6
|
-
|
7
|
-
The example will fail if no instance receives the specified message.
|
8
|
-
|
9
|
-
Scenario: expect a message on any instance of a class
|
10
|
-
Given a file named "example_spec.rb" with:
|
11
|
-
"""ruby
|
12
|
-
describe "expect_any_instance_of" do
|
13
|
-
before do
|
14
|
-
expect_any_instance_of(Object).to receive(:foo).and_return(:return_value)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "verifies that one instance of the class receives the message" do
|
18
|
-
o = Object.new
|
19
|
-
expect(o.foo).to eq(:return_value)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "fails unless an instance receives that message" do
|
23
|
-
end
|
24
|
-
end
|
25
|
-
"""
|
26
|
-
When I run `rspec example_spec.rb`
|
27
|
-
Then the output should contain "2 examples, 1 failure"
|
28
|
-
And the output should contain "1) expect_any_instance_of fails unless an instance receives that message"
|
29
|
-
|
30
|
-
Scenario: expect a message on any instance of a class (should syntax)
|
31
|
-
Given a file named "example_spec.rb" with:
|
32
|
-
"""ruby
|
33
|
-
describe "any_instance.should_receive" do
|
34
|
-
it "verifies that one instance of the class receives the message" do
|
35
|
-
Object.any_instance.should_receive(:foo).and_return(:return_value)
|
36
|
-
|
37
|
-
o = Object.new
|
38
|
-
expect(o.foo).to eq(:return_value)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
"""
|
42
|
-
When I run `rspec example_spec.rb`
|
43
|
-
Then the examples should all pass
|