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