| 148 |
lars |
1 |
.. index::
|
|
|
2 |
single: Cookbook; Default Mock Expectations
|
|
|
3 |
|
|
|
4 |
Default Mock Expectations
|
|
|
5 |
=========================
|
|
|
6 |
|
|
|
7 |
Often in unit testing, we end up with sets of tests which use the same object
|
|
|
8 |
dependency over and over again. Rather than mocking this class/object within
|
|
|
9 |
every single unit test (requiring a mountain of duplicate code), we can
|
|
|
10 |
instead define reusable default mocks within the test case's ``setup()``
|
|
|
11 |
method. This even works where unit tests use varying expectations on the same
|
|
|
12 |
or similar mock object.
|
|
|
13 |
|
|
|
14 |
How this works, is that you can define mocks with default expectations. Then,
|
|
|
15 |
in a later unit test, you can add or fine-tune expectations for that specific
|
|
|
16 |
test. Any expectation can be set as a default using the ``byDefault()``
|
|
|
17 |
declaration.
|