Mocks (and mock expectations) are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations. mock, which wraps a stub in a spy, allowing callers to supply basic function implementations and assert against those calls. Stub is an object that holds predefined data and uses it to answer calls during tests. In every unit test, there should be one unit under test. Stubs, mocks, and spies make tests more robust and less prone to breakage should dependent codes evolve or have their internals modified. It also mentions Martin Fowler and it explains a bit of software testing history. That's OK, I'll try to explain it. Spies are stubs that also record some information based on how they were called. â SinonJS Stubs One form of this might be an email service that records how many messages it was sent. Stubs are highly configurable, and can do a lot more than this, but most follow these basic ideas. Mocks are what we are talking about here: objects pre-programmed with expectations which ⦠A mock will fail your test if it is not used as expected. Spies and stubs can be used independently; any protocol implementation may be wrapped by a spy, and stubs need not themselves be spies. The classification of mocks and stubs (as well as fakes and spies) is a bit inconsistent across different authors, but I think the most present, albeit simplified, description would be the following. What are mocks? Mocks (and mock expectations) are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations. Stubs and mocks are created the same. Stub. (I've tried to have these samples follow Fowler's samples so that the two articles can be read together easily). Well, perhaps not. One form of this might be an email service that records how many messages it was sent. The difference between the two is that a stub only returns a preset result when called, while a mock needs to have expectations set on the method calls it expects to receive. Where other JavaScript testing libraries would lean on a specific stub/spy library like Sinon - Standalone test spies, stubs and mocks for JavaScript. You should take care when using mocks â itâs easy to overlook spies and stubs when mocks can do everything they can, but mocks also easily make your tests overly specific, which leads to brittle tests that break easily. â ⦠It explains the differences between and subtleties of dummies, test doubles, stubs, spies, (true) mocks and fakes. Stubs and mocks are probably the most common ones, but there are other variations of them called Dummies and Spies. Mocks are what we are talking about here: objects pre-programmed with expectations which ⦠It can create stubs, mocks, and spies. Mocks are dynamic wrappers for dependencies used in tests. Works with any unit testing framework., Jest comes with stubs, mocks and spies out of the box. It is used when we cannot or donât want to involve objects that would answer with real data or have undesirable side effects. Spies are stubs that also record some information based on how they were called. An example can be an object that needs to grab some data from the database to respond to a method call. Mocks and Stubs aren't Spies. At this point, we all know the difference between mocks and stubs... right? Spy A spy is a fake function that keeps track of arguments, returns value, the value of this and exception is thrown (if any) for all its calls . When to Use Mocks. Mocks should only be used for the method under test. And if I do a poor job you can always go read the article.