Skip to content
Newer
Older
100644 68 lines (48 sloc) 2.35 KB
6551f1a @dchelimsky docs
dchelimsky authored Feb 21, 2011
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:
f757599 @dchelimsky add README to features dir
dchelimsky authored Jul 1, 2010
40
b342ebf @dchelimsky words
dchelimsky authored Oct 10, 2010
41 describe Account do
42 context "when closed" do
6551f1a @dchelimsky docs
dchelimsky authored Feb 21, 2011
43 it "logs an 'account closed' message" do
b342ebf @dchelimsky words
dchelimsky authored Oct 10, 2010
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
6551f1a @dchelimsky docs
dchelimsky authored Feb 21, 2011
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
f757599 @dchelimsky add README to features dir
dchelimsky authored Jul 2, 2010
59 ## Issues
60
b342ebf @dchelimsky words
dchelimsky authored Oct 10, 2010
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).
Something went wrong with that request. Please try again.