Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation Correction and Improved Explanation #2004

Merged
merged 1 commit into from Apr 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions docs/index.md
Expand Up @@ -137,19 +137,16 @@ function getTodos(listId, callback) {
});
}
```

To test this function without triggering network activity we could replace `jQuery.ajax`
A unit test should not actually trigger a function's network activity. To test `getTodos()` without triggering its network activity, use the `sinon.replace()` method to replace the `jQuery.ajax` method in your test. Restore the `jQuery.ajax` method after your test by calling `sinon.restore()` in your test runner's `after()` function.

```javascript
after(function () {
// When the test either fails or passes, restore the original
// jQuery ajax function (Sinon.JS also provides tools to help
// test frameworks automate clean-up like this)
jQuery.ajax.restore();
sinon.restore();
});

it('makes a GET request for todo items', function () {
sinon.replace(jQuery, 'ajax', sinon.fake());

getTodos(42, sinon.fake());

assert(jQuery.ajax.calledWithMatch({ url: '/todo/42/items' }));
Expand Down