Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / doc / src / documentation / mocks / index.page
blob69b9c2a34de634e58a89e6e20810b1c57d45fc41
1 ---
2 title: Mocks 
3 ---
4 h2. Mocks, Stubs and Partials
6 RSpec will create mocks and stubs for you at runtime, or attach stub/mock behaviour
7 to any of your real objects (Partial Mock/Stub). Note that the underlying implementation
8 for mocks and stubs is the same, and you can therefore intermingle mock and stub
9 behaviour in either. There is a semantic difference in how they are created, however,
10 which can help clarify the role it is playing within a given spec.
12 h3. Mocks
14 <a href="mocks.html">Mocks</a> are objects that
15 allow you to set and verify expectations. They are very useful for specifying how the subject of
16 the spec interacts with its collaborators. This approach is widely known as "interaction
17 testing".
19 Mocks are also very powerful (though less widely understood) as a design tool. As you are
20 driving the implementation of a given class, Mocks provide an amorphous
21 collaborator that can change in behaviour as quickly as you can write an expectation in your
22 spec. These changes are tantamount to designing the interface to a collaborator that often
23 does not yet exist. As the shape of the class being specified becomes more clear, so do the
24 requirements for its collaborators - often leading to the discovery of new types that are
25 needed in your system.
27 Read "Endo-Testing":http://www.mockobjects.com/files/endotesting.pdf for a much
28 more in depth description of this process.
30 h3. Stubs
32 <a href="stubs.html">Stubs</a> are objects that allow you to set "stub" responses to
33 messages. As Martin Fowler points out on his site, "Mocks aren't stubs":http://www.martinfowler.com/articles/mocksArentStubs.html. Paraphrasing Fowler's paraphrasing
34 of Gerard Meszaros: Stubs provide canned responses to messages they might receive in a test, while
35 mocks allow you to specify and, subsequently, verify that certain messages should be received during
36 the execution of a test.
38 h3. Partials
40 RSpec also supports "Partials":partial_mocks.html, allowing you to add stub/mock behaviour
41 to instances of your existing classes. This is generally
42 something to be avoided, because changes to the class can have ripple effects on
43 seemingly unrelated specs. When specs fail due to these ripple effects, the fact
44 that some methods are being mocked can make it difficult to understand why a
45 failure is occurring.
47 That said, "Partials":partial_mocks.html do allow you to expect and
48 verify interactions with class methods such as <code>:find</code> and <code>:create</code>
49 on Ruby on Rails model classes.
51 h3. Notes
53 There are many different viewpoints about the meaning of mocks and stubs. If you are interested
54 in learning more, here is some recommended reading:
56 * Mock Objects: "http://www.mockobjects.com/":http://www.mockobjects.com/
57 * Endo-Testing:"http://www.mockobjects.com/files/endotesting.pdf":http://www.mockobjects.com/files/endotesting.pdf
58 * Mock Roles, Not Objects: "http://www.mockobjects.com/files/mockrolesnotobjects.pdf":http://www.mockobjects.com/files/mockrolesnotobjects.pdf
59 * Test Double Patterns: "http://xunitpatterns.com/Test%20Double%20Patterns.html":http://xunitpatterns.com/Test%20Double%20Patterns.html
60 * Mocks aren't stubs: "http://www.martinfowler.com/articles/mocksArentStubs.html":http://www.martinfowler.com/articles/mocksArentStubs.html