1 min read

Pretend objects

Etienne Marais

Pretend objects are things that provide the strategy for you to be able to write tests in an isolated way. Test Doubles is a generic term for any kind of pretend object used in place of the real object for testing purposes. This is a short and concise list of what these 4 pretend objects are.

Original source: martinfowler/mocksArentStubs

Armed with these tools you would be able to combine them in any way possible to test your code thoroughly.

Dummy

Dummy objects are passed around but never used. They mostly act as parameter fills and help you contain your unit tests to just test a single thing.

Fakes

Fakes have actual working implementations, but usually take some shortcut which makes them not ready for production. It might capture a mail send to not go via an api call but mark the mail as getting sent as if the api call happened.

Stubs

Stubs provide canned answers to calls made during the test. Usually responds to just what’s been programmed for the test. Typically won’t let a test fail. Stubs may also record information about calls like if an email has been marked as sent or maybe how many messages have been sent.

Mocks

Mocks are objects pre programmed with expectations which form a specification of the calls they are expected to receive. Mocks expect certain things to be called on the object and will let a unit test fail if the expectations aren’t met.