Skip to content

Commit

Permalink
Documentation (#2004)
Browse files Browse the repository at this point in the history
1. replace jQuery.ajax.restore(); with sinon.restore();
2. improve explanation
  • Loading branch information
76784 authored and mantoni committed Apr 3, 2019
1 parent 3812a7d commit 2430fd9
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
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

0 comments on commit 2430fd9

Please sign in to comment.