Mocking? Why When & How to Mock in your Tests
- Dominant language
- Rust
- Stars
- 826
- Forks
- 133
- PR merge metrics
- No merged PRs in 30d
Description
## 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:
``` js
```
**_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_)
- The wikipedia article is quite good: https://en.wikipedia.org/wiki/Mock_object
Contributor guide
Research direction
No file or test is identified in the issue. Review the existing book structure and the draft’s mocking section first, then determine the expected scope for a useful JavaScript example; done should be a complete, beginner-oriented explanation of why and how to mock, with a working example and accurate references.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- documentation, testing
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100