Diasparsoft Logo Let's write software that people understand.

Home | Contact

Training

JUnit support

Outsourcing

The work that Diasparsoft did for us was outstanding. We are now using the software on a daily basis and their work could well have a dramatic impact on the Reds' organization in the near future.Cincinnati Reds Baseball Club.


Publications

Tips & Tricks

Diasparsoft Toolkit

What is Diaspar?

Interesting Bits RSS

Home » Archives » January 2005 » When to fake? When to mock?

[Previous entry: "Framework or platform?"] [Next entry: "CUSEC this weekend"]

01/03/2005: "When to fake? When to mock?"


In working with Pat Welsh I had occasion to describe in a rather pithy way when to fake and when to mock, so I thought I'd share that with you.

To be clear, I mean "mock" in the sense of interaction-based testing with mock objects, and I mean "fake" in the sense of state-based testing with fakes or stubs. Google if you need a stronger definition of those terms, but they suit me for now.

So then, I want to test some object in isolation from the rest of the system. In broad terms, I need to write tests to answer two essential questions:

  1. Am I using the objects around me correctly? Am I invoking the right methods at the right times with the right parameters?
  2. Am I reacting to the objects around me correctly? Do I handle errors reasonably? Do I respond well to the answers they send me?
For the first kind of test, I generally mock; and for the second kind of test, I generally fake.

Put another way, I lean on interaction-based testing to verify how my object talks to its collaborators; but I lean on state-based testing to verify how well that object listens.