Skip to content

Commit

Permalink
Add a best practice sentence about data models
Browse files Browse the repository at this point in the history
It should always be possible to use real instances for data only classes.
Specifically mention data models in the best practices section as not needing
mocks.

PiperOrigin-RevId: 560634157
  • Loading branch information
natebosch authored and copybara-github committed Aug 28, 2023
1 parent 70aabfc commit f5abf11
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,14 @@ throwOnMissingStub(cat);

Testing with real objects is preferred over testing with mocks - if you can
construct a real instance for your tests, you should! If there are no calls to
[`verify`] in your test, it is a strong signal that you may not need mocks at all,
though it's also OK to use a `Mock` like a stub. When it's not possible to use
the real object, a tested implementation of a fake is the next best thing - it's
more likely to behave similarly to the real class than responses stubbed out in
tests. Finally an object which `extends Fake` using manually overridden methods
is preferred over an object which `extends Mock` used as either a stub or a
mock.
[`verify`] in your test, it is a strong signal that you may not need mocks at
all, though it's also OK to use a `Mock` like a stub. Data models never need to
be mocked if they can be constructed with stubbed data. When it's not possible
to use the real object, a tested implementation of a fake is the next best
thing; it's more likely to behave similarly to the real class than responses
stubbed out in tests. Finally an object which `extends Fake` using manually
overridden methods is preferred over an object which `extends Mock` used as
either a stub or a mock.

A class which `extends Mock` should _never_ stub out its own responses with
`when` in its constructor or anywhere else. Stubbed responses should be defined
Expand Down

0 comments on commit f5abf11

Please sign in to comment.