sinon
这是一个使用and的单元测试解决方案mocha
:
index.js
:
var request = require("request");
function func(arg1, arg2) {
request.get("https://github.com/mrdulin", function(error, response, body) {
console.log(arg1, arg2);
if (error) {
console.log(error);
} else if (response.status === 200) {
console.log(response);
} else {
console.log("others");
}
});
}
exports.func = func;
index.spec.js
:
var foo = require("./");
var sinon = require("sinon");
var request = require("request");
describe("func", () => {
afterEach(() => {
sinon.restore();
});
it("should handle error", () => {
const logSpy = sinon.spy(console, "log");
const getStub = sinon.stub(request, "get");
foo.func(1, 2);
const mError = new Error("network error");
getStub.yield(mError, null, null);
sinon.assert.calledWith(
getStub,
"https://github.com/mrdulin",
sinon.match.func
);
sinon.assert.calledWith(logSpy.firstCall, 1, 2);
sinon.assert.calledWith(logSpy.secondCall, mError);
});
it("should handle response", () => {
const logSpy = sinon.spy(console, "log");
const getStub = sinon.stub(request, "get");
foo.func(1, 2);
const mResponse = { status: 200 };
getStub.yield(null, mResponse, null);
sinon.assert.calledWith(
getStub,
"https://github.com/mrdulin",
sinon.match.func
);
sinon.assert.calledWith(logSpy.firstCall, 1, 2);
sinon.assert.calledWith(logSpy.secondCall, mResponse);
});
it("should handle other situation", () => {
const logSpy = sinon.spy(console, "log");
const getStub = sinon.stub(request, "get");
foo.func(1, 2);
const mResponse = { status: 500 };
getStub.yield(null, mResponse, null);
sinon.assert.calledWith(
getStub,
"https://github.com/mrdulin",
sinon.match.func
);
sinon.assert.calledWith(logSpy.firstCall, 1, 2);
sinon.assert.calledWith(logSpy.secondCall, "others");
});
});
覆盖率 100% 的单元测试结果:
func
1 2
Error: network error
at Context.it (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/src/stackoverflow/37764363/index.spec.js:1:4103)
at callFn (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:387:21)
at Test.Runnable.run (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:379:7)
at Runner.runTest (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:535:10)
at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:653:12
at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:447:14)
at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:457:7
at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:362:14)
at Immediate._onImmediate (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:425:5)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
✓ should handle error (48ms)
1 2
{ status: 200 }
✓ should handle response
1 2
others
✓ should handle other situation
3 passing (58ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
index.spec.js | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
源代码:https ://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/37764363