rspec-mocks 2.0.0.beta.8 → 2.0.0.beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +1 -1
- data/Rakefile +10 -10
- data/VERSION +1 -1
- data/features/mocks/block_local_expectations.feature +5 -5
- data/features/mocks/mix_stubs_and_mocks.feature +2 -2
- data/features/mocks/warn_when_expectation_is_set_on_nil.feature +6 -6
- data/features/stubs/stub_implementation.feature +2 -2
- data/lib/rspec/mocks.rb +112 -137
- data/lib/rspec/mocks/argument_expectation.rb +1 -1
- data/lib/rspec/mocks/argument_matchers.rb +1 -1
- data/lib/rspec/mocks/error_generator.rb +2 -2
- data/lib/rspec/mocks/errors.rb +1 -1
- data/lib/rspec/mocks/extensions/instance_exec.rb +2 -2
- data/lib/rspec/mocks/extensions/object.rb +1 -1
- data/lib/rspec/mocks/message_expectation.rb +3 -3
- data/lib/rspec/mocks/method_double.rb +3 -3
- data/lib/rspec/mocks/methods.rb +1 -1
- data/lib/rspec/mocks/mock.rb +9 -6
- data/lib/rspec/mocks/order_group.rb +1 -1
- data/lib/rspec/mocks/proxy.rb +9 -8
- data/lib/rspec/mocks/space.rb +1 -1
- data/lib/rspec/mocks/spec_methods.rb +13 -15
- data/lib/rspec/mocks/version.rb +1 -1
- data/rspec-mocks.gemspec +11 -11
- data/spec/rspec/mocks/and_yield_spec.rb +4 -4
- data/spec/rspec/mocks/any_number_of_times_spec.rb +2 -2
- data/spec/rspec/mocks/argument_expectation_spec.rb +3 -3
- data/spec/rspec/mocks/at_least_spec.rb +7 -7
- data/spec/rspec/mocks/at_most_spec.rb +5 -5
- data/spec/rspec/mocks/bug_report_10260_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_15719_spec.rb +3 -3
- data/spec/rspec/mocks/bug_report_830_spec.rb +2 -2
- data/spec/rspec/mocks/double_spec.rb +1 -1
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +14 -14
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +1 -1
- data/spec/rspec/mocks/hash_not_including_matcher_spec.rb +1 -1
- data/spec/rspec/mocks/mock_ordering_spec.rb +4 -4
- data/spec/rspec/mocks/mock_space_spec.rb +2 -2
- data/spec/rspec/mocks/mock_spec.rb +32 -25
- data/spec/rspec/mocks/multiple_return_value_spec.rb +12 -12
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +3 -3
- data/spec/rspec/mocks/null_object_mock_spec.rb +3 -3
- data/spec/rspec/mocks/once_counts_spec.rb +4 -4
- data/spec/rspec/mocks/options_hash_spec.rb +4 -4
- data/spec/rspec/mocks/partial_mock_spec.rb +9 -9
- data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +3 -3
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +3 -3
- data/spec/rspec/mocks/precise_counts_spec.rb +3 -3
- data/spec/rspec/mocks/record_messages_spec.rb +2 -2
- data/spec/rspec/mocks/stash_spec.rb +1 -1
- data/spec/rspec/mocks/stub_chain_spec.rb +1 -1
- data/spec/rspec/mocks/stub_implementation_spec.rb +1 -1
- data/spec/rspec/mocks/stub_spec.rb +1 -1
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +2 -2
- data/spec/rspec/mocks/twice_counts_spec.rb +5 -5
- data/spec/spec_helper.rb +4 -4
- metadata +10 -10
data/README.markdown
CHANGED
data/Rakefile
CHANGED
@@ -6,15 +6,15 @@ begin
|
|
6
6
|
require 'jeweler'
|
7
7
|
Jeweler::Tasks.new do |gem|
|
8
8
|
gem.name = "rspec-mocks"
|
9
|
-
gem.version =
|
10
|
-
gem.summary = "rspec-mocks-#{
|
11
|
-
gem.description = "
|
9
|
+
gem.version = RSpec::Mocks::Version::STRING
|
10
|
+
gem.summary = "rspec-mocks-#{RSpec::Mocks::Version::STRING}"
|
11
|
+
gem.description = "RSpec's 'test double' framework, with support for stubbing and mocking"
|
12
12
|
gem.email = "dchelimsky@gmail.com;chad.humphries@gmail.com"
|
13
13
|
gem.homepage = "http://github.com/rspec/mocks"
|
14
14
|
gem.authors = ["David Chelimsky", "Chad Humphries"]
|
15
15
|
gem.rubyforge_project = "rspec"
|
16
|
-
gem.add_development_dependency 'rspec-core',
|
17
|
-
gem.add_development_dependency 'rspec-expectations',
|
16
|
+
gem.add_development_dependency 'rspec-core', RSpec::Mocks::Version::STRING
|
17
|
+
gem.add_development_dependency 'rspec-expectations', RSpec::Mocks::Version::STRING
|
18
18
|
gem.post_install_message = <<-EOM
|
19
19
|
#{"*"*50}
|
20
20
|
|
@@ -34,20 +34,20 @@ end
|
|
34
34
|
namespace :gem do
|
35
35
|
desc "push to gemcutter"
|
36
36
|
task :push => :build do
|
37
|
-
system "gem push pkg/rspec-mocks-#{
|
37
|
+
system "gem push pkg/rspec-mocks-#{RSpec::Mocks::Version::STRING}.gem"
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
begin
|
42
42
|
require 'rspec/core/rake_task'
|
43
|
-
|
43
|
+
RSpec::Core::RakeTask.new(:spec)
|
44
44
|
|
45
|
-
|
45
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
46
46
|
spec.rcov = true
|
47
47
|
spec.rcov_opts = %[--exclude "core,expectations,gems/*,spec/resources,spec/spec,spec/spec_helper.rb,db/*,/Library/Ruby/*,config/*" --text-summary --sort coverage]
|
48
48
|
end
|
49
49
|
rescue LoadError
|
50
|
-
puts "
|
50
|
+
puts "RSpec core or one of its dependencies is not installed. Install it with: gem install rspec-meta"
|
51
51
|
end
|
52
52
|
|
53
53
|
begin
|
@@ -68,7 +68,7 @@ end
|
|
68
68
|
require 'rake/rdoctask'
|
69
69
|
Rake::RDocTask.new do |rdoc|
|
70
70
|
rdoc.rdoc_dir = 'rdoc'
|
71
|
-
rdoc.title = "rspec-mocks #{
|
71
|
+
rdoc.title = "rspec-mocks #{RSpec::Mocks::Version::STRING}"
|
72
72
|
rdoc.rdoc_files.include('README*')
|
73
73
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
74
74
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.0.0.beta.
|
1
|
+
2.0.0.beta.9
|
@@ -1,7 +1,7 @@
|
|
1
1
|
Feature: block local expectations
|
2
2
|
|
3
3
|
Background:
|
4
|
-
Given a file named "account.rb" with:
|
4
|
+
Given a file named "lib/account.rb" with:
|
5
5
|
"""
|
6
6
|
class Account
|
7
7
|
def self.create
|
@@ -14,7 +14,7 @@ Feature: block local expectations
|
|
14
14
|
"""
|
15
15
|
|
16
16
|
Scenario: passing example
|
17
|
-
Given a file named "account_passing_spec.rb" with:
|
17
|
+
Given a file named "spec/account_passing_spec.rb" with:
|
18
18
|
"""
|
19
19
|
require 'account'
|
20
20
|
|
@@ -29,12 +29,12 @@ Feature: block local expectations
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
"""
|
32
|
-
When I run "rspec account_passing_spec.rb"
|
32
|
+
When I run "rspec ./spec/account_passing_spec.rb"
|
33
33
|
Then I should see "1 example, 0 failures"
|
34
34
|
|
35
35
|
Scenario: failing example
|
36
36
|
|
37
|
-
Given a file named "account_failing_spec.rb" with:
|
37
|
+
Given a file named "spec/account_failing_spec.rb" with:
|
38
38
|
"""
|
39
39
|
require 'account'
|
40
40
|
|
@@ -50,5 +50,5 @@ Feature: block local expectations
|
|
50
50
|
end
|
51
51
|
"""
|
52
52
|
|
53
|
-
When I run "rspec account_failing_spec.rb"
|
53
|
+
When I run "rspec ./spec/account_failing_spec.rb"
|
54
54
|
Then I should see "1 example, 1 failure"
|
@@ -8,7 +8,7 @@ Feature: Spec and test together
|
|
8
8
|
"""
|
9
9
|
require 'rspec/expectations'
|
10
10
|
|
11
|
-
|
11
|
+
RSpec.configure do |config|
|
12
12
|
config.mock_framework = :rspec
|
13
13
|
end
|
14
14
|
|
@@ -24,5 +24,5 @@ Feature: Spec and test together
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
"""
|
27
|
-
When I run "rspec stub_and_mocks_spec.rb -fs"
|
27
|
+
When I run "rspec ./stub_and_mocks_spec.rb -fs"
|
28
28
|
Then I should see "received :foo with unexpected arguments"
|
@@ -3,7 +3,7 @@ Feature: warn when expectation is set on nil
|
|
3
3
|
Scenario: nil instance variable
|
4
4
|
Given a file named "example_spec.rb" with:
|
5
5
|
"""
|
6
|
-
|
6
|
+
RSpec.configure {|c| c.mock_with :rspec}
|
7
7
|
describe "something" do
|
8
8
|
it "does something" do
|
9
9
|
@i_do_not_exist.should_receive(:foo)
|
@@ -11,13 +11,13 @@ Feature: warn when expectation is set on nil
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
"""
|
14
|
-
When I run "rspec example_spec.rb"
|
14
|
+
When I run "rspec ./example_spec.rb"
|
15
15
|
Then I should see "An expectation of :foo was set on nil"
|
16
16
|
|
17
17
|
Scenario: allow
|
18
18
|
Given a file named "example_spec.rb" with:
|
19
19
|
"""
|
20
|
-
|
20
|
+
RSpec.configure {|c| c.mock_with :rspec}
|
21
21
|
describe "something" do
|
22
22
|
it "does something" do
|
23
23
|
allow_message_expectations_on_nil
|
@@ -26,13 +26,13 @@ Feature: warn when expectation is set on nil
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
"""
|
29
|
-
When I run "rspec example_spec.rb"
|
29
|
+
When I run "rspec ./example_spec.rb"
|
30
30
|
Then I should not see "An expectation"
|
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
|
"""
|
35
|
-
|
35
|
+
RSpec.configure {|c| c.mock_with :rspec}
|
36
36
|
describe "something" do
|
37
37
|
it "does something (foo)" do
|
38
38
|
allow_message_expectations_on_nil
|
@@ -45,6 +45,6 @@ Feature: warn when expectation is set on nil
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
"""
|
48
|
-
When I run "rspec example_spec.rb"
|
48
|
+
When I run "rspec ./example_spec.rb"
|
49
49
|
Then I should see "An expectation of :bar"
|
50
50
|
And I should not see "An expectation of :foo"
|
@@ -6,7 +6,7 @@ Feature: stub implementation
|
|
6
6
|
Scenario: stub implementation
|
7
7
|
Given a file named "stub_implementation_spec.rb" with:
|
8
8
|
"""
|
9
|
-
|
9
|
+
RSpec.configure do |c|
|
10
10
|
c.mock_with :rspec
|
11
11
|
end
|
12
12
|
|
@@ -26,5 +26,5 @@ Feature: stub implementation
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
"""
|
29
|
-
When I run "rspec stub_implementation_spec.rb"
|
29
|
+
When I run "rspec ./stub_implementation_spec.rb"
|
30
30
|
Then I should see "1 example, 0 failures"
|
data/lib/rspec/mocks.rb
CHANGED
@@ -2,109 +2,74 @@ require 'rspec/mocks/framework'
|
|
2
2
|
require 'rspec/mocks/extensions/object'
|
3
3
|
require 'rspec/mocks/version'
|
4
4
|
|
5
|
-
module
|
6
|
-
# ==
|
5
|
+
module RSpec
|
6
|
+
# == Test Doubles
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# behaviour in either dynamically generated mocks or your pre-existing classes.
|
12
|
-
# There is a semantic difference in how they are created, however,
|
13
|
-
# which can help clarify the role it is playing within a given spec.
|
8
|
+
# A Test Double is an object that stands in for a real object in a test.
|
9
|
+
# RSpec creates test doubles that support method stubs and message
|
10
|
+
# expectations.
|
14
11
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# Mocks are objects that allow you to set and verify expectations that they will
|
18
|
-
# receive specific messages during run time. They are very useful for specifying how the subject of
|
19
|
-
# the spec interacts with its collaborators. This approach is widely known as "interaction
|
20
|
-
# testing".
|
21
|
-
#
|
22
|
-
# Mocks are also very powerful as a design tool. As you are
|
23
|
-
# driving the implementation of a given class, Mocks provide an anonymous
|
24
|
-
# collaborator that can change in behaviour as quickly as you can write an expectation in your
|
25
|
-
# spec. This flexibility allows you to design the interface of a collaborator that often
|
26
|
-
# does not yet exist. As the shape of the class being specified becomes more clear, so do the
|
27
|
-
# requirements for its collaborators - often leading to the discovery of new types that are
|
28
|
-
# needed in your system.
|
29
|
-
#
|
30
|
-
# Read Endo-Testing[http://www.mockobjects.com/files/endotesting.pdf] for a much
|
31
|
-
# more in depth description of this process.
|
32
|
-
#
|
33
|
-
# == Stubs
|
34
|
-
#
|
35
|
-
# Stubs are objects that allow you to set "stub" responses to
|
36
|
-
# messages. As Martin Fowler points out on his site,
|
37
|
-
# mocks_arent_stubs[http://www.martinfowler.com/articles/mocksArentStubs.html].
|
38
|
-
# Paraphrasing Fowler's paraphrasing
|
39
|
-
# of Gerard Meszaros: Stubs provide canned responses to messages they might receive in a test, while
|
40
|
-
# mocks allow you to specify and, subsquently, verify that certain messages should be received during
|
41
|
-
# the execution of a test.
|
42
|
-
#
|
43
|
-
# == Partial Mocks/Stubs
|
44
|
-
#
|
45
|
-
# RSpec also supports partial mocking/stubbing, allowing you to add stub/mock behaviour
|
46
|
-
# to instances of your existing classes. This is generally
|
47
|
-
# something to be avoided, because changes to the class can have ripple effects on
|
48
|
-
# seemingly unrelated specs. When specs fail due to these ripple effects, the fact
|
49
|
-
# that some methods are being mocked can make it difficult to understand why a
|
50
|
-
# failure is occurring.
|
51
|
-
#
|
52
|
-
# That said, partials do allow you to expect and
|
53
|
-
# verify interactions with class methods such as +#find+ and +#create+
|
54
|
-
# on Ruby on Rails model classes.
|
55
|
-
#
|
56
|
-
# == Further Reading
|
57
|
-
#
|
58
|
-
# There are many different viewpoints about the meaning of mocks and stubs. If you are interested
|
59
|
-
# in learning more, here is some recommended reading:
|
60
|
-
#
|
61
|
-
# * Mock Objects: http://www.mockobjects.com/
|
62
|
-
# * Endo-Testing: http://www.mockobjects.com/files/endotesting.pdf
|
63
|
-
# * Mock Roles, Not Objects: http://www.mockobjects.com/files/mockrolesnotobjects.pdf
|
64
|
-
# * Test Double Patterns: http://xunitpatterns.com/Test%20Double%20Patterns.html
|
65
|
-
# * Mocks aren't stubs: http://www.martinfowler.com/articles/mocksArentStubs.html
|
66
|
-
#
|
67
|
-
# == Creating a Mock
|
68
|
-
#
|
69
|
-
# You can create a mock in any specification (or setup) using:
|
12
|
+
# book = double("book")
|
70
13
|
#
|
71
|
-
#
|
14
|
+
# == Method Stubs
|
72
15
|
#
|
73
|
-
#
|
74
|
-
# option is +:null_object+. Setting this to true instructs the mock to ignore
|
75
|
-
# any messages it hasn’t been told to expect – and quietly return itself. For example:
|
16
|
+
# A method stub is an implementation that returns a pre-determined value.
|
76
17
|
#
|
77
|
-
# double("
|
18
|
+
# book = double("book")
|
19
|
+
# double.stub(:title) { "The RSpec Book" }
|
20
|
+
# double.title => "The RSpec Book"
|
78
21
|
#
|
79
|
-
#
|
22
|
+
# When we declare a stub, we way we "stubbing" a method.
|
80
23
|
#
|
81
|
-
#
|
24
|
+
# == Message Expectations
|
82
25
|
#
|
83
|
-
#
|
26
|
+
# A message expectation is an expectation that the test double will receive a
|
27
|
+
# message some time before the example ends. If the message is received, the
|
28
|
+
# expectation is satisfied. If not, the example fails.
|
84
29
|
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
# == Creating a Partial Mock
|
91
|
-
#
|
92
|
-
# You don't really "create" a partial mock, you simply add method stubs and/or
|
93
|
-
# mock expectations to existing classes and objects:
|
94
|
-
#
|
95
|
-
# Factory.should_receive(:find).with(id).and_return(value)
|
96
|
-
# obj.stub(:to_i).and_return(3)
|
97
|
-
# etc ...
|
98
|
-
#
|
99
|
-
# == Expecting Messages
|
100
|
-
#
|
101
|
-
# my_mock.should_receive(:sym)
|
102
|
-
# my_mock.should_not_receive(:sym)
|
30
|
+
# registrar = double("registrar")
|
31
|
+
# validator.should_receive(:validate).with("02134")
|
32
|
+
# zipcode = Zipcode.new("02134", validator)
|
33
|
+
# zipcode.valid?
|
103
34
|
#
|
35
|
+
# When we declare a message expectation, we way we "mocking" a method.
|
36
|
+
#
|
37
|
+
# == Mock Objects and Test Stubs
|
38
|
+
#
|
39
|
+
# The names Mock Object and Test Stub suggest specialized Test Doubles. i.e.
|
40
|
+
# Test Stub evokes Test Double that only supports method stubs, and a Mock
|
41
|
+
# Object evokes a Test Double that only supports message expectations, or
|
42
|
+
# sometimes supports message expectations in addition to method stubs.
|
43
|
+
#
|
44
|
+
# There is a lot of overlapping nomenclature here, and there are many
|
45
|
+
# variations of these patterns (fakes, spies, etc). Keep in mind that most of
|
46
|
+
# the time we're talking about method-level concepts that are variations of
|
47
|
+
# method stubs and message expectations, and we're applying to them to _one_
|
48
|
+
# generic kind of object: a Test Double.
|
49
|
+
#
|
50
|
+
# == Test-Specific Extension
|
51
|
+
#
|
52
|
+
# a.k.a. Partial Stub/Mock, a Test-Specific Extension is an extension of a
|
53
|
+
# real object in a system that is instrumented with test-double like
|
54
|
+
# behaviour in the context of a test. This technique is very common in Ruby
|
55
|
+
# because we often see class objects acting as global namespaces for methods.
|
56
|
+
# For example, in Rails:
|
57
|
+
#
|
58
|
+
# person = double("person")
|
59
|
+
# Person.stub(:find) { person }
|
60
|
+
#
|
61
|
+
# In this case we're instrumenting Person to return the person object we've
|
62
|
+
# defined whenever it receives the +find+ message. We can do this with any
|
63
|
+
# object in a system because RSpec adds the +stub+ and +should_receive+
|
64
|
+
# methods to every object. When we use either, RSpec replaces the method
|
65
|
+
# we're stubbing or mocking with it's own test-double-like method. At the
|
66
|
+
# end of the example, RSpec verifies any message expectations, and then
|
67
|
+
# restores the original methods.
|
68
|
+
#
|
104
69
|
# == Expecting Arguments
|
105
70
|
#
|
106
|
-
#
|
107
|
-
#
|
71
|
+
# double.should_receive(:msg).with(*args)
|
72
|
+
# double.should_not_receive(:msg).with(*args)
|
108
73
|
#
|
109
74
|
# == Argument Matchers
|
110
75
|
#
|
@@ -112,68 +77,71 @@ module Rspec
|
|
112
77
|
# using == by default. In cases in which you want to specify things about the arguments
|
113
78
|
# rather than the arguments themselves, you can use any of RSpec's Expression Matchers.
|
114
79
|
# They don't all make syntactic sense (they were primarily designed for use with
|
115
|
-
#
|
80
|
+
# RSpec::Expectations), but you are free to create your own custom RSpec::Matchers.
|
116
81
|
#
|
117
|
-
#
|
82
|
+
# RSpec::Mocks does provide one additional Matcher method named #ducktype.
|
118
83
|
#
|
119
|
-
# In addition,
|
84
|
+
# In addition, RSpec::Mocks adds some keyword Symbols that you can use to
|
120
85
|
# specify certain kinds of arguments:
|
121
86
|
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
87
|
+
# double.should_receive(:msg).with(no_args())
|
88
|
+
# double.should_receive(:msg).with(any_args())
|
89
|
+
# double.should_receive(:msg).with(1, kind_of(Numeric), "b") #2nd argument can any kind of Numeric
|
90
|
+
# double.should_receive(:msg).with(1, boolean(), "b") #2nd argument can true or false
|
91
|
+
# double.should_receive(:msg).with(1, /abc/, "b") #2nd argument can be any String matching the submitted Regexp
|
92
|
+
# double.should_receive(:msg).with(1, anything(), "b") #2nd argument can be anything at all
|
93
|
+
# double.should_receive(:msg).with(1, ducktype(:abs, :div), "b")
|
129
94
|
# #2nd argument can be object that responds to #abs and #div
|
130
95
|
#
|
131
96
|
# == Receive Counts
|
132
97
|
#
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
98
|
+
# double.should_receive(:msg).once
|
99
|
+
# double.should_receive(:msg).twice
|
100
|
+
# double.should_receive(:msg).exactly(n).times
|
101
|
+
# double.should_receive(:msg).at_least(:once)
|
102
|
+
# double.should_receive(:msg).at_least(:twice)
|
103
|
+
# double.should_receive(:msg).at_least(n).times
|
104
|
+
# double.should_receive(:msg).at_most(:once)
|
105
|
+
# double.should_receive(:msg).at_most(:twice)
|
106
|
+
# double.should_receive(:msg).at_most(n).times
|
107
|
+
# double.should_receive(:msg).any_number_of_times
|
143
108
|
#
|
144
109
|
# == Ordering
|
145
110
|
#
|
146
|
-
#
|
147
|
-
#
|
111
|
+
# double.should_receive(:msg).ordered
|
112
|
+
# double.should_receive(:other_msg).ordered
|
148
113
|
# #This will fail if the messages are received out of order
|
149
114
|
#
|
150
115
|
# == Setting Reponses
|
151
116
|
#
|
152
|
-
# Whether you are setting a
|
153
|
-
# object precisely how to respond
|
117
|
+
# Whether you are setting a message expectation or a method stub, you can
|
118
|
+
# tell the object precisely how to respond. The most generic way is to pass
|
119
|
+
# a block to +stub+ or +should_receive+:
|
120
|
+
#
|
121
|
+
# double.should_receive(:msg) { value }
|
122
|
+
#
|
123
|
+
# When the double receives the +msg+ message, it evaluates the block and returns
|
124
|
+
# the result.
|
154
125
|
#
|
155
|
-
#
|
156
|
-
#
|
126
|
+
# double.should_receive(:msg).and_return(value)
|
127
|
+
# double.should_receive(:msg).exactly(3).times.and_return(value1, value2, value3)
|
157
128
|
# # returns value1 the first time, value2 the second, etc
|
158
|
-
#
|
159
|
-
# my_mock.should_receive(:sym).and_raise(error)
|
129
|
+
# double.should_receive(:msg).and_raise(error)
|
160
130
|
# #error can be an instantiated object or a class
|
161
131
|
# #if it is a class, it must be instantiable with no args
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
132
|
+
# double.should_receive(:msg).and_throw(:msg)
|
133
|
+
# double.should_receive(:msg).and_yield(values,to,yield)
|
134
|
+
# double.should_receive(:msg).and_yield(values,to,yield).and_yield(some,other,values,this,time)
|
165
135
|
# # for methods that yield to a block multiple times
|
166
136
|
#
|
167
|
-
# Any of these responses can be applied to a stub as well
|
168
|
-
# not support any qualifiers about the message received (i.e. you can't specify arguments
|
169
|
-
# or receive counts):
|
137
|
+
# Any of these responses can be applied to a stub as well
|
170
138
|
#
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
139
|
+
# double.stub(:msg).and_return(value)
|
140
|
+
# double.stub(:msg).and_return(value1, value2, value3)
|
141
|
+
# double.stub(:msg).and_raise(error)
|
142
|
+
# double.stub(:msg).and_throw(:msg)
|
143
|
+
# double.stub(:msg).and_yield(values,to,yield)
|
144
|
+
# double.stub(:msg).and_yield(values,to,yield).and_yield(some,other,values,this,time)
|
177
145
|
#
|
178
146
|
# == Arbitrary Handling
|
179
147
|
#
|
@@ -182,21 +150,28 @@ module Rspec
|
|
182
150
|
# to come with an Array argument that has a specific length, but you don't care
|
183
151
|
# what is in it. You could do this:
|
184
152
|
#
|
185
|
-
#
|
153
|
+
# double.should_receive(:msg) do |arg|
|
186
154
|
# arg.should be_an_istance_of(Array)
|
187
155
|
# arg.length.should == 7
|
188
156
|
# end
|
189
157
|
#
|
190
|
-
# Note that this would fail if the number of arguments received was different from
|
191
|
-
# the number of block arguments (in this case 1).
|
192
|
-
#
|
193
158
|
# == Combining Expectation Details
|
194
159
|
#
|
195
160
|
# Combining the message name with specific arguments, receive counts and responses
|
196
161
|
# you can get quite a bit of detail in your expectations:
|
197
162
|
#
|
198
|
-
#
|
163
|
+
# double.should_receive(:<<).with("illegal value").once.and_raise(ArgumentError)
|
164
|
+
#
|
165
|
+
# == Further Reading
|
166
|
+
#
|
167
|
+
# There are many different viewpoints about the meaning of mocks and stubs. If you are interested
|
168
|
+
# in learning more, here is some recommended reading:
|
169
|
+
#
|
170
|
+
# * Mock Objects: http://www.mockobjects.com/
|
171
|
+
# * Endo-Testing: http://www.mockobjects.com/files/endotesting.pdf
|
172
|
+
# * Mock Roles, Not Objects: http://www.mockobjects.com/files/mockrolesnotobjects.pdf
|
173
|
+
# * Test Double Patterns: http://xunitpatterns.com/Test%20Double%20Patterns.html
|
174
|
+
# * Mocks aren't stubs: http://www.martinfowler.com/articles/mocksArentStubs.html
|
199
175
|
module Mocks
|
200
176
|
end
|
201
|
-
|
202
177
|
end
|