diff --git a/README.md b/README.md index 977eb70..1b94fb1 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,14 @@ mock.onGet(/\/users\/\d+/).reply(function(config) { }); ``` +Chaining is also supported + +```js +mock + .onGet('/users').reply(200, users) + .onGet('/posts').reply(200, posts); +``` + Mocking any request to a given url ```js diff --git a/index.js b/index.js index 04e338d..edf2a9f 100644 --- a/index.js +++ b/index.js @@ -54,6 +54,7 @@ MockAdapter.prototype.onAny = function(matcher) { verbs.forEach(function(verb) { _this.matchers[verb].push(handler); }); + return _this; } }; }; @@ -71,6 +72,7 @@ verbs.forEach(function(method) { return { reply: function reply(code, response, headers) { _this.matchers[method].push([matcher, code, response, headers]); + return _this; } }; }; diff --git a/test/mock_adapter_test.js b/test/mock_adapter_test.js index 95a7989..3cef6a0 100644 --- a/test/mock_adapter_test.js +++ b/test/mock_adapter_test.js @@ -163,6 +163,16 @@ describe('MockAdapter', function() { expect(newInstance.defaults.adapter).to.equal(adapter); }); + it('can chain calls to add mock handlers', function() { + mock + .onGet('/foo').reply(200) + .onAny('/bar').reply(404) + .onPost('/baz').reply(500); + + expect(mock.matchers['get']).to.not.be.empty; + expect(mock.matchers['post']).to.not.be.empty; + }); + context('on the default instance', function() { afterEach(function() { axios.defaults.adapter = undefined;