📗 Our Book on Full-Stack Web Application Development covering User Experience (UX) Design, Mobile/Offline/Security First, Progressive Enhancement, Continuous Integration/Deployment, Testing (UX/TDD/BDD), Performance-Driven-Development and much more!
_Situation_: You want to use a service (API) made by someone else in your project, but you don't want to call that API inside your tests because the network is slow/unreliable or simply because the service requires multi-step (user-input) authentication (e.g: OAuth2) and you don't want to go through that in your tests.
What?
In unit testing a _Mock_ is used to simulate an object or response (from an API) in a controlled way.
How?
Lets say you are building an app called "_BroLee?_" that gives people recommendations on when they should carry an umbrella to wear based on the weather conditions in their location.
Example:
_nock_ is made by @pgte and is the _gold standard_ in simplified libraries.
Notes
Sinon.js is the Grandaddy of Mocking in JavaScript: http://sinonjs.org (I've tried to teach it to complete beginners and most found it confusing, stick to nock and keep it simple)
Why?
_Situation_: You want to use a service (API) made by someone else in your project, but you don't want to call that API inside your tests because the network is slow/unreliable or simply because the service requires multi-step (user-input) authentication (e.g: OAuth2) and you don't want to go through that in your tests.
What?
In unit testing a _Mock_ is used to simulate an object or response (from an API) in a controlled way.
How?
Lets say you are building an app called "_BroLee?_" that gives people recommendations on when they should carry an umbrella to wear based on the weather conditions in their location.
Example:
_nock_ is made by @pgte and is the _gold standard_ in simplified libraries.
Notes