?????????????????????????????????nodejs??????Promises????з?????mock??web???????????
????????
?????????????????mocha.
??????
????????????????????????(????? done ) ???ɡ?
describe('User'?? function() {
describe('#save()'?? function() {
it('should save without error'?? function(done) {
var user = new User('Luna');
user.save(done);
});
});
});
????Mocha?????趨?????2s???????е???????2s???????timeout???????????????????????????
???????????
????mocha -t 10000
????API?
describe('async'?? function () {
this.timeout(10000);
it('async'?? function (done) {
lib.async(function (result) {
done();
});
});
});
????Promises
?????????????????????? Promise ????????? Chai as Promised ??
'use strict';
require('should');
require('chai').use(require('chai-as-promised'));
describe('test/app/simple/promise.test.js'?? () => {
it('#Promise'?? ()=> {
(new Promise(function(resolve) {
resolve(10);
})).should.be.a.Promise();
(10).should.not.be.a.Promise();
});
it('#fulfilled'?? () => {
return new Promise(resolve => resolve(10))
.should.be.fulfilled();
});
it('#fulfilledWith'?? () => {
return new Promise((resolve) => resolve(10))
.should.be.fulfilledWith(10);
});
it('#rejected'?? () => {
return new Promise((resolve?? reject) => reject(new Error('boom')))
.should.be.rejected();
});
});
??????з???
??????????????????????????????????????з????????????????з?????????????????????з?????????????????????????Ч??
????????????javascript???????з?????????????????????з????δ??????( exports )???????
???????????????????? cal.js ??????? add ???????????????
????function add(a?? b) {
????return a + b;
????}
????????????з?????????????? cal ?????????????? rewire ?????
????rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing.
??????????????з????????????
????it('1 + 1 should equals 2'?? function () {
????let cal = rewire('./cal.js');
????let add = cal.__get__('add');
????add(1?? 1).should.be.eql(2);
????});
????????????з????? rewire ????????????б????????? ???????
???????????????????飬
// lib/myModules.js
// With rewire you can change all these variables
var fs = require("fs")??
path = "/somewhere/on/the/disk";
function readSomethingFromFileSystem(cb) {
console.log("Reading from file system ...");
fs.readFile(path?? "utf8"?? cb);
}
exports.readSomethingFromFileSystem = readSomethingFromFileSystem;

???????ò??????б?????
myModule.__set__("path"?? "/dev/null");
myModule.__get__("path"); // = '/dev/null'
??????????mock??
var fsMock = {
readFile: function (path?? encoding?? cb) {
expect(path).to.equal("/somewhere/on/the/disk");
cb(null?? "Success!");
}
};
myModule.__set__("fs"?? fsMock);
????mock
????mock???????????У??????Щ??????????????????????????????????????????????????????????nodejs?г????mock?????? muk ?? sinon ?????sinon???????????????÷???
?????????????????
function once(fn) {
var returnValue?? called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this?? arguments);
}
return returnValue;
};
}
Spy
it("calls the original function"?? function () {
var spy = sinon.spy();
var proxy = once(spy);
proxy();
assert(spy.called);
});
Stubs
it("returns the return value from the original function"?? function () {
var stub = sinon.stub().returns(42);
var proxy = once(stub);
assert.equals(proxy()?? 42);
});
Mocks
it("returns the return value from the original function"?? function () {
var myAPI = { method: function () {} };
var mock = sinon.mock(myAPI);
mock.expects("method").once().returns(42);
var proxy = once(myAPI.method);
assert.equals(proxy()?? 42);
mock.verify();
});
????web
??????????? SuperTest ??????????????????????????
????????
????????????
?????????????CI?? Continuous integration??????????????????????????????????????????????????μ???????????????????????????????????????????????????????????????????
????github ????? ci ??????? Travis CI ???????????????????????????????
??????????
??????дjs???????????У?鶴?????????????????????淶??????????????????????????????jsУ?鶴???? JSLint ?? JSHint ?? JSCS ?? ESLint ?? ????????????????ο? A Comparison of JavaScript Linting Tools ???????? ESLint ??
???????????????????????? ci???????? ci ???????????????ɡ??? eslint ?????
????"scripts": {
????// ..
????"lint": "eslint ."??
????"cov": "istanbul cover ."??
????"ci": "tnpm run lint && TEST_TIMEOUT=60000 istanbul cover ."
????}??
????????????????????????飬??????????????????????????????????????????1????ж??
????д??????
??????????д???nodejs?????ο???????????????nodejs??????????????????д??????в????????????????????????????????????????????????и????????90%??????????????????????????90???