JS_Dev
JS_Dev

Reputation: 547

Simple definition of stub, spy, fake and mock in unit testing

I'm quite new to unit testing. I've read around on here as well as done some Googling, but am still a bit confused as to the meaning of each of the four meanings. I came across the following definition which help....

Stub - stubs are minimal implementations of interfaces or base classes

Spy - a spy will record which members were invoked

Fake - more complex, a fake may resemble a production implementation

Mock - A mock is usually dynamically created by a mock library and depending on its configuration, a mock can behave like a dummy, a stub, or a spy

However, I'd like to simplify the meaning (if possible) and ask a few questions.

  1. Do all of the above only relate to functions, or can they be objects or any other type?
  2. Stub - Is Stubbing bascially a way to provide dummy info instead of making the calling to the actual database? So for example, if I had an API call, instead of actually making a call to the API, I just instead make a GET request to a JSON file which is in my tests folder which has some dummy data in, and use that instead of making the API call?
  3. Spy - so is this a way of tracking what happens to a function for example. Meaning you follow when it's called, where it gets passed around to?
  4. Fake - Is this for example a function which you create inside the test file to mimic the real function or be a simpified version of the actual function?

Thanks in advance.

Upvotes: 17

Views: 9665

Answers (2)

Rohde Fischer
Rohde Fischer

Reputation: 1480

There are multiple attempts at definitions. To my knowledge there is no fully consistent definition, probably due to the fact that mocking frameworks defines things slightly differently. Martin Fowler lists the following (https://martinfowler.com/bliki/TestDouble.html):

  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an InMemoryTestDatabase is a good example).
  • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
  • Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
  • Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting.

Fowler has them from this page: http://xunitpatterns.com/Test%20Double.html where you can read about them in a bit more detail.

Upvotes: 25

Jocke
Jocke

Reputation: 2294

  1. No, they apply to more then just functions
  2. Yes
  3. Kind of, in most cases you will spy on an object
  4. Yes

The implementation and nomenclatur will depend of what framework you use.

Upvotes: 1

Related Questions