sinon - GradedJestRisk/js-training GitHub Wiki
-
spies: function that records arguments for all its calls (also: return value, this and exception thrown)
- can wrap methods that already exists
- can also be anonymous
- stubs: functions (spies) with pre-programmed behavior
-
mocks: functions (spies) with pre-programmed
- behavior (like stubs)
- expectations
- spies: offer information about function calls, without affecting their behavior
- stubs: completely replace the function:
- return a specific value
- throw an exception
- mocks: combining both spies and stubs
- fakes: ?
2 ways:
- use native API
- use sinon-chai plugin
API: Test stubs are functions (spies) with pre-programmed behavior.
API Mocks 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.
The rule of thumb is: if you wouldn’t add an assertion for some specific call, don’t mock it. Use a stub instead.