rspec-mocks 2.11.3 → 3.11.1
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/.document +1 -1
- data/.yardopts +1 -1
- data/Changelog.md +989 -21
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +260 -73
- data/lib/rspec/mocks/any_instance/chain.rb +58 -24
- data/lib/rspec/mocks/any_instance/error_generator.rb +31 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +31 -0
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +23 -30
- data/lib/rspec/mocks/any_instance/message_chains.rb +38 -15
- data/lib/rspec/mocks/any_instance/proxy.rb +116 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +155 -59
- data/lib/rspec/mocks/any_instance/stub_chain.rb +33 -19
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +10 -12
- data/lib/rspec/mocks/any_instance.rb +11 -81
- data/lib/rspec/mocks/argument_list_matcher.rb +59 -37
- data/lib/rspec/mocks/argument_matchers.rb +233 -149
- data/lib/rspec/mocks/configuration.rb +212 -0
- data/lib/rspec/mocks/error_generator.rb +304 -49
- data/lib/rspec/mocks/example_methods.rb +361 -22
- data/lib/rspec/mocks/instance_method_stasher.rb +146 -0
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/have_received.rb +134 -0
- data/lib/rspec/mocks/matchers/receive.rb +133 -0
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +82 -0
- data/lib/rspec/mocks/matchers/receive_messages.rb +77 -0
- data/lib/rspec/mocks/message_chain.rb +87 -0
- data/lib/rspec/mocks/message_expectation.rb +648 -314
- data/lib/rspec/mocks/method_double.rb +185 -58
- data/lib/rspec/mocks/method_reference.rb +202 -0
- data/lib/rspec/mocks/minitest_integration.rb +68 -0
- data/lib/rspec/mocks/mutate_const.rb +339 -0
- data/lib/rspec/mocks/object_reference.rb +149 -0
- data/lib/rspec/mocks/order_group.rb +48 -7
- data/lib/rspec/mocks/proxy.rb +405 -74
- data/lib/rspec/mocks/space.rb +219 -15
- data/lib/rspec/mocks/standalone.rb +2 -2
- data/lib/rspec/mocks/syntax.rb +325 -0
- data/lib/rspec/mocks/targets.rb +124 -0
- data/lib/rspec/mocks/test_double.rb +125 -57
- data/lib/rspec/mocks/verifying_double.rb +121 -0
- data/lib/rspec/mocks/verifying_message_expectation.rb +54 -0
- data/lib/rspec/mocks/verifying_proxy.rb +220 -0
- data/lib/rspec/mocks/version.rb +3 -1
- data/lib/rspec/mocks.rb +121 -27
- data.tar.gz.sig +0 -0
- metadata +178 -253
- metadata.gz.sig +3 -0
- data/features/README.md +0 -67
- 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 -60
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -27
- data/features/message_expectations/README.md +0 -69
- data/features/message_expectations/any_instance.feature +0 -21
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/expect_message.feature +0 -94
- 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 -47
- data/features/method_stubs/any_instance.feature +0 -133
- data/features/method_stubs/as_null_object.feature +0 -35
- data/features/method_stubs/simple_return_value.feature +0 -64
- data/features/method_stubs/stub_chain.feature +0 -51
- data/features/method_stubs/stub_implementation.feature +0 -26
- data/features/method_stubs/to_ary.feature +0 -47
- data/features/outside_rspec/configuration.feature +0 -82
- data/features/outside_rspec/standalone.feature +0 -32
- data/features/step_definitions/additional_cli_steps.rb +0 -4
- data/features/stubbing_constants/README.md +0 -62
- data/features/stubbing_constants/stub_defined_constant.feature +0 -79
- data/features/stubbing_constants/stub_undefined_constant.feature +0 -50
- data/features/support/env.rb +0 -6
- data/lib/rspec/mocks/errors.rb +0 -12
- data/lib/rspec/mocks/extensions/instance_exec.rb +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -23
- data/lib/rspec/mocks/extensions/psych.rb +0 -23
- data/lib/rspec/mocks/framework.rb +0 -21
- data/lib/rspec/mocks/methods.rb +0 -155
- data/lib/rspec/mocks/mock.rb +0 -7
- data/lib/rspec/mocks/serialization.rb +0 -34
- data/lib/rspec/mocks/stashed_instance_method.rb +0 -60
- data/lib/rspec/mocks/stub_const.rb +0 -332
- data/lib/spec/mocks.rb +0 -2
- data/spec/rspec/mocks/and_yield_spec.rb +0 -114
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -40
- data/spec/rspec/mocks/any_instance_spec.rb +0 -877
- data/spec/rspec/mocks/any_number_of_times_spec.rb +0 -30
- data/spec/rspec/mocks/argument_expectation_spec.rb +0 -34
- data/spec/rspec/mocks/at_least_spec.rb +0 -142
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -53
- data/spec/rspec/mocks/bug_report_10260_spec.rb +0 -8
- data/spec/rspec/mocks/bug_report_10263_spec.rb +0 -25
- data/spec/rspec/mocks/bug_report_11545_spec.rb +0 -32
- data/spec/rspec/mocks/bug_report_496_spec.rb +0 -17
- data/spec/rspec/mocks/bug_report_600_spec.rb +0 -22
- data/spec/rspec/mocks/bug_report_7611_spec.rb +0 -16
- data/spec/rspec/mocks/bug_report_8165_spec.rb +0 -31
- data/spec/rspec/mocks/bug_report_830_spec.rb +0 -21
- data/spec/rspec/mocks/bug_report_957_spec.rb +0 -22
- data/spec/rspec/mocks/double_spec.rb +0 -12
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -95
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -90
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -103
- data/spec/rspec/mocks/mock_space_spec.rb +0 -58
- data/spec/rspec/mocks/mock_spec.rb +0 -730
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -119
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -62
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -106
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/partial_mock_spec.rb +0 -171
- data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -142
- 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 -111
- data/spec/rspec/mocks/stash_spec.rb +0 -27
- data/spec/rspec/mocks/stashed_instance_method_spec.rb +0 -53
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -154
- data/spec/rspec/mocks/stub_const_spec.rb +0 -334
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -247
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -47
- data/spec/rspec/mocks/test_double_spec.rb +0 -57
- data/spec/rspec/mocks/to_ary_spec.rb +0 -40
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks_spec.rb +0 -51
- data/spec/spec_helper.rb +0 -21
data/features/README.md
DELETED
@@ -1,67 +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 to create one:
|
19
|
-
|
20
|
-
double_account = double("Account")
|
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.
|
25
|
-
|
26
|
-
## Method Stubs
|
27
|
-
|
28
|
-
A method stub is an instruction to an object (real or test double) to return a
|
29
|
-
known value in response to a message:
|
30
|
-
|
31
|
-
die.stub(:roll) { 3 }
|
32
|
-
|
33
|
-
This tells the `die` object to return the value `3` when it receives the `roll`
|
34
|
-
message.
|
35
|
-
|
36
|
-
## Message Expectations
|
37
|
-
|
38
|
-
A message expectation is an expectation that an object should receive a
|
39
|
-
specific message during the course of a code example:
|
40
|
-
|
41
|
-
describe Account do
|
42
|
-
context "when closed" do
|
43
|
-
it "logs an 'account closed' message" do
|
44
|
-
logger = double()
|
45
|
-
account = Account.new
|
46
|
-
account.logger = logger
|
47
|
-
|
48
|
-
logger.should_receive(:account_closed).with(account)
|
49
|
-
|
50
|
-
account.close
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
This example specifies that the `account` object sends the `logger` the
|
56
|
-
`account_closed` message (with itself as an argument) when it receives the
|
57
|
-
`close` message.
|
58
|
-
|
59
|
-
## Issues
|
60
|
-
|
61
|
-
The documentation for rspec-mocks is a work in progress. We'll be adding
|
62
|
-
Cucumber features over time, and clarifying existing ones. If you have
|
63
|
-
specific features you'd like to see added, find the existing documentation
|
64
|
-
incomplete or confusing, or, better yet, wish to write a missing Cucumber
|
65
|
-
feature yourself, please [submit an
|
66
|
-
issue](http://github.com/rspec/rspec-mocks/issues) or a [pull
|
67
|
-
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
|
-
obj.stub(:foo).with(:bar) do |arg|
|
8
|
-
#do something for :bar
|
9
|
-
end
|
10
|
-
obj.stub(: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
|
-
obj.should_receive(:message).with('an argument')
|
21
|
-
|
22
|
-
If more control is needed, one can use a block
|
23
|
-
|
24
|
-
obj.should_receive(:message) do |arg1, arg2|
|
25
|
-
# set expectations about the args in this block
|
26
|
-
# and optionally set a return value
|
27
|
-
end
|
@@ -1,60 +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
|
-
"""
|
8
|
-
describe "stubbed explicit arguments" do
|
9
|
-
it "works on stubs" do
|
10
|
-
object = Object.new
|
11
|
-
object.stub(:foo).with(:this) do |arg|
|
12
|
-
"got this"
|
13
|
-
end
|
14
|
-
object.stub(:foo).with(:that) do |arg|
|
15
|
-
"got that"
|
16
|
-
end
|
17
|
-
|
18
|
-
object.foo(:this).should eq("got this")
|
19
|
-
object.foo(:that).should eq("got that")
|
20
|
-
end
|
21
|
-
|
22
|
-
it "works on doubles and expectations" do
|
23
|
-
object = double('foo')
|
24
|
-
object.should_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
|
-
"""
|
36
|
-
describe "stubbed multiple explicit arguments" do
|
37
|
-
it "works on stubs" do
|
38
|
-
object = Object.new
|
39
|
-
object.stub(:foo).with(:this) do |arg|
|
40
|
-
"got this"
|
41
|
-
end
|
42
|
-
object.stub(:foo).with(:this, :that) do |arg1, arg2|
|
43
|
-
"got this and that"
|
44
|
-
end
|
45
|
-
|
46
|
-
object.foo(:this).should eq("got this")
|
47
|
-
object.foo(:this, :that).should eq("got this and that")
|
48
|
-
end
|
49
|
-
|
50
|
-
it "works on mocks" do
|
51
|
-
object = double('foo')
|
52
|
-
object.should_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"
|
60
|
-
|
@@ -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
|
-
"""
|
11
|
-
describe "stubbed anything() args spec" do
|
12
|
-
it "works" do
|
13
|
-
object = Object.new
|
14
|
-
object.stub(:foo).with(anything) do
|
15
|
-
"anything"
|
16
|
-
end
|
17
|
-
|
18
|
-
object.foo(1).should eq("anything")
|
19
|
-
object.foo(:that).should 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
|
-
"""
|
29
|
-
describe "stubbed any_args() args spec" do
|
30
|
-
it "works" do
|
31
|
-
object = Object.new
|
32
|
-
object.stub(:foo).with(any_args) do
|
33
|
-
"anything"
|
34
|
-
end
|
35
|
-
|
36
|
-
object.foo(1).should eq("anything")
|
37
|
-
object.foo(:that).should eq("anything")
|
38
|
-
object.foo.should 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
|
-
"""
|
48
|
-
describe "stubbed no_args() args spec" do
|
49
|
-
it "works for no args" do
|
50
|
-
object = Object.new
|
51
|
-
object.stub(:foo).with(no_args) do
|
52
|
-
"nothing"
|
53
|
-
end
|
54
|
-
object.stub(:foo).with(anything) do
|
55
|
-
"something"
|
56
|
-
end
|
57
|
-
|
58
|
-
object.foo(:that).should eq("something")
|
59
|
-
object.foo.should 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
|
-
"""
|
69
|
-
describe "stubbed no_args() args spec for expectations" do
|
70
|
-
it "works for no args" do
|
71
|
-
object = Object.new
|
72
|
-
object.should_receive(:foo).with(no_args)
|
73
|
-
|
74
|
-
object.foo
|
75
|
-
end
|
76
|
-
it "fails for args" do
|
77
|
-
object = Object.new
|
78
|
-
object.should_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,27 +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
|
-
"""
|
10
|
-
describe "stubbed an_instance_of() args spec" do
|
11
|
-
it "works" do
|
12
|
-
object = Object.new
|
13
|
-
object.stub(:foo).with(an_instance_of(Symbol)) do
|
14
|
-
"symbol"
|
15
|
-
end
|
16
|
-
object.stub(:foo).with(an_instance_of(String)) do
|
17
|
-
"string"
|
18
|
-
end
|
19
|
-
|
20
|
-
object.foo("bar").should eq("string")
|
21
|
-
object.foo(:that).should 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"
|
27
|
-
|
@@ -1,69 +0,0 @@
|
|
1
|
-
### Basics
|
2
|
-
|
3
|
-
# create a double
|
4
|
-
obj = double()
|
5
|
-
|
6
|
-
# expect a message
|
7
|
-
obj.should_receive(:message)
|
8
|
-
|
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)
|
13
|
-
|
14
|
-
These forms are somewhat interchangeable. The difference is that the
|
15
|
-
block contents are evaluated lazily when the `obj` receives the
|
16
|
-
`message` message, whereas the others are evaluated as they are read.
|
17
|
-
|
18
|
-
### Fake implementation
|
19
|
-
|
20
|
-
obj.should_receive(:message) do |arg1, arg2|
|
21
|
-
# set expectations about the args in this block
|
22
|
-
# and set a return value
|
23
|
-
end
|
24
|
-
|
25
|
-
### Raising/Throwing
|
26
|
-
|
27
|
-
obj.should_receive(:message).and_raise("this error")
|
28
|
-
obj.should_receive(:message).and_throw(:this_symbol)
|
29
|
-
|
30
|
-
You can also use the block format:
|
31
|
-
|
32
|
-
obj.should_receive(:message) { raise "this error" }
|
33
|
-
obj.should_receive(:message) { throw :this_symbol }
|
34
|
-
|
35
|
-
### Argument constraints
|
36
|
-
|
37
|
-
#### Explicit arguments
|
38
|
-
|
39
|
-
obj.should_receive(:message).with('an argument')
|
40
|
-
obj.should_receive(:message).with('more_than', 'one_argument')
|
41
|
-
|
42
|
-
#### Argument matchers
|
43
|
-
|
44
|
-
obj.should_receive(:message).with(anything())
|
45
|
-
obj.should_receive(:message).with(an_instance_of(Money))
|
46
|
-
obj.should_receive(:message).with(hash_including(:a => 'b'))
|
47
|
-
|
48
|
-
#### Regular expressions
|
49
|
-
|
50
|
-
obj.should_receive(:message).with(/abc/)
|
51
|
-
|
52
|
-
### Counts
|
53
|
-
|
54
|
-
obj.should_receive(:message).once
|
55
|
-
obj.should_receive(:message).twice
|
56
|
-
obj.should_receive(:message).exactly(3).times
|
57
|
-
|
58
|
-
obj.should_receive(:message).at_least(:once)
|
59
|
-
obj.should_receive(:message).at_least(:twice)
|
60
|
-
obj.should_receive(:message).at_least(n).times
|
61
|
-
|
62
|
-
obj.should_receive(:message).at_most(:once)
|
63
|
-
obj.should_receive(:message).at_most(:twice)
|
64
|
-
obj.should_receive(:message).at_most(n).times
|
65
|
-
|
66
|
-
### Ordering
|
67
|
-
|
68
|
-
obj.should_receive(:one).ordered
|
69
|
-
obj.should_receive(:two).ordered
|
@@ -1,21 +0,0 @@
|
|
1
|
-
Feature: expect a message on any instance of a class
|
2
|
-
|
3
|
-
Use `any_instance.should_receive` to set an expectation that one (and only
|
4
|
-
one) instance of a class receives a message before the example is completed.
|
5
|
-
|
6
|
-
The spec will fail if no instance receives a message.
|
7
|
-
|
8
|
-
Scenario: expect a message on any instance of a class
|
9
|
-
Given a file named "example_spec.rb" with:
|
10
|
-
"""
|
11
|
-
describe "any_instance.should_receive" do
|
12
|
-
it "verifies that one instance of the class receives the message" do
|
13
|
-
Object.any_instance.should_receive(:foo).and_return(:return_value)
|
14
|
-
|
15
|
-
o = Object.new
|
16
|
-
o.foo.should eq(:return_value)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
"""
|
20
|
-
When I run `rspec example_spec.rb`
|
21
|
-
Then the examples should all pass
|
@@ -1,55 +0,0 @@
|
|
1
|
-
Feature: block local expectations
|
2
|
-
|
3
|
-
Background:
|
4
|
-
Given a file named "lib/account.rb" with:
|
5
|
-
"""
|
6
|
-
class Account
|
7
|
-
def self.create
|
8
|
-
yield new
|
9
|
-
end
|
10
|
-
|
11
|
-
def opening_balance(amount, currency)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
"""
|
15
|
-
|
16
|
-
Scenario: passing example
|
17
|
-
Given a file named "spec/account_spec.rb" with:
|
18
|
-
"""
|
19
|
-
require 'account'
|
20
|
-
|
21
|
-
describe "account DSL" do
|
22
|
-
it "it succeeds when the block local receives the given call" do
|
23
|
-
account = double("Account")
|
24
|
-
Account.should_receive(:create).and_yield(account) do |account|
|
25
|
-
account.should_receive(:opening_balance).with(100, :USD)
|
26
|
-
end
|
27
|
-
Account.create do |account|
|
28
|
-
account.opening_balance 100, :USD
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
"""
|
33
|
-
When I run `rspec spec/account_spec.rb`
|
34
|
-
Then the output should contain "1 example, 0 failures"
|
35
|
-
|
36
|
-
Scenario: failing example
|
37
|
-
|
38
|
-
Given a file named "spec/account_spec.rb" with:
|
39
|
-
"""
|
40
|
-
require 'account'
|
41
|
-
|
42
|
-
describe "account DSL" do
|
43
|
-
it "fails when the block local does not receive the expected call" do
|
44
|
-
Account.should_receive(:create).and_yield do |account|
|
45
|
-
account.should_receive(:opening_balance).with(100, :USD)
|
46
|
-
end
|
47
|
-
Account.create do |account|
|
48
|
-
# opening_balance is not called here
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
"""
|
53
|
-
|
54
|
-
When I run `rspec spec/account_spec.rb`
|
55
|
-
Then the output should contain "1 example, 1 failure"
|
@@ -1,94 +0,0 @@
|
|
1
|
-
Feature: expect a message
|
2
|
-
|
3
|
-
Use should_receive() to set an expectation that a receiver should receive a
|
4
|
-
message before the example is completed.
|
5
|
-
|
6
|
-
Scenario: expect a message
|
7
|
-
Given a file named "spec/account_spec.rb" with:
|
8
|
-
"""
|
9
|
-
require "account"
|
10
|
-
|
11
|
-
describe Account do
|
12
|
-
context "when closed" do
|
13
|
-
it "logs an account closed message" do
|
14
|
-
logger = double("logger")
|
15
|
-
account = Account.new
|
16
|
-
account.logger = logger
|
17
|
-
|
18
|
-
logger.should_receive(:account_closed)
|
19
|
-
|
20
|
-
account.close
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
"""
|
25
|
-
And a file named "lib/account.rb" with:
|
26
|
-
"""
|
27
|
-
class Account
|
28
|
-
attr_accessor :logger
|
29
|
-
|
30
|
-
def close
|
31
|
-
logger.account_closed
|
32
|
-
end
|
33
|
-
end
|
34
|
-
"""
|
35
|
-
When I run `rspec spec/account_spec.rb`
|
36
|
-
Then the output should contain "1 example, 0 failures"
|
37
|
-
|
38
|
-
Scenario: expect a message with an argument
|
39
|
-
Given a file named "spec/account_spec.rb" with:
|
40
|
-
"""
|
41
|
-
require "account"
|
42
|
-
|
43
|
-
describe Account do
|
44
|
-
context "when closed" do
|
45
|
-
it "logs an account closed message" do
|
46
|
-
logger = double("logger")
|
47
|
-
account = Account.new
|
48
|
-
account.logger = logger
|
49
|
-
|
50
|
-
logger.should_receive(:account_closed).with(account)
|
51
|
-
|
52
|
-
account.close
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
"""
|
57
|
-
And a file named "lib/account.rb" with:
|
58
|
-
"""
|
59
|
-
class Account
|
60
|
-
attr_accessor :logger
|
61
|
-
|
62
|
-
def close
|
63
|
-
logger.account_closed(self)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
"""
|
67
|
-
When I run `rspec spec/account_spec.rb`
|
68
|
-
Then the output should contain "1 example, 0 failures"
|
69
|
-
|
70
|
-
Scenario: provide a return value
|
71
|
-
Given a file named "message_expectation_spec.rb" with:
|
72
|
-
"""
|
73
|
-
describe "a message expectation" do
|
74
|
-
context "with a return value" do
|
75
|
-
context "specified in a block" do
|
76
|
-
it "returns the specified value" do
|
77
|
-
receiver = double("receiver")
|
78
|
-
receiver.should_receive(:message) { :return_value }
|
79
|
-
receiver.message.should eq(:return_value)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context "specified with and_return" do
|
84
|
-
it "returns the specified value" do
|
85
|
-
receiver = double("receiver")
|
86
|
-
receiver.should_receive(:message).and_return(:return_value)
|
87
|
-
receiver.message.should eq(:return_value)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
"""
|
93
|
-
When I run `rspec message_expectation_spec.rb`
|
94
|
-
Then the output should contain "2 examples, 0 failures"
|