-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from rafaelcaviquioli/173-throw-exception-on-…
…no-match Create onNoMatch=throwException option to throw exception when could not find mock for requested url
- Loading branch information
Showing
4 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var axios = require("axios"); | ||
var expect = require("chai").expect; | ||
|
||
var MockAdapter = require("../src"); | ||
|
||
describe("onNoMatch=throwException option tests (requires Node)", function () { | ||
var instance; | ||
var mock; | ||
|
||
beforeEach(function () { | ||
instance = axios.create(); | ||
mock = new MockAdapter(instance, { onNoMatch: "throwException" }); | ||
}); | ||
|
||
it("allows selective mocking", function () { | ||
mock.onGet("/foo").reply(200, "bar"); | ||
mock.onGet("/error").reply(200, "success"); | ||
|
||
return Promise.all([ | ||
instance.get("/foo").then(function (response) { | ||
expect(response.status).to.equal(200); | ||
expect(response.data).to.equal("bar"); | ||
}), | ||
instance.get("/error").then(function (response) { | ||
expect(response.status).to.equal(200); | ||
expect(response.data).to.equal("success"); | ||
}), | ||
]); | ||
}); | ||
|
||
it("handles errors correctly when could not find mock for requested url", function () { | ||
var expectedUrl = 'http://127.0.0.1/unexistent_path'; | ||
var expectedMethod = "get"; | ||
|
||
return instance | ||
.get(expectedUrl) | ||
.then(function () { | ||
// The server should've returned an error | ||
expect(false).to.be.true; | ||
}) | ||
.catch(function (error) { | ||
expect(error).to.have.nested.property("isCouldNotFindMockError", true); | ||
expect(error).to.have.nested.property("method", expectedMethod); | ||
expect(error).to.have.nested.property("url", expectedUrl); | ||
expect(error.message).to.contain("Could not find mock for"); | ||
expect(error.message).to.contain(expectedMethod); | ||
expect(error.message).to.contain(expectedUrl); | ||
}); | ||
}); | ||
}); |