Here's a description of each one followed by with real world sample.
- Dummy - just bogus values to satisfy the
API
.
Example: If you're testing a method of a class which requires many mandatory parameters in a constructor which have no effect on your test, then you may create dummy objects for the purpose of creating new instances of a class.
- Fake - create a test implementation of a class which may have a dependency on some external infrastructure. (It's good practice that your unit test does NOT actually interact with external infrastructure.)
Example: Create fake implementation for accessing a database, replace it with
in-memory
collection.
- Stub - override methods to return hard-coded values, also referred to as
state-based
.
Example: Your test class depends on a method
Calculate()
taking 5 minutes to complete. Rather than wait for 5 minutes you can replace its real implementation with stub that returns hard-coded values; taking only a small fraction of the time.
- Mock - very similar to
Stub
butinteraction-based
rather than state-based. This means you don't expect fromMock
to return some value, but to assume that specific order of method calls are made.
Example: You're testing a user registration class. After calling
Save
, it should callSendConfirmationEmail
.
Stubs
and Mocks
are actually sub types of Mock
, both swap real implementation with test implementation, but for different, specific reasons.