?????????????????????????? ????£?
???????????????Node?????????????????????????????????????????????Χ?Щ???????????н?????????????????????????????????????????????е?????????????в?????????????????????????
???????????????????????????????????????????????壬????????????????????Mocha??chai Assert?????????????????????????????????е????Hour???????
????????????????о??????????????????е?????????
????????????????????????????????????????????????????£????????????????????????????????????????????
????????????????????????????????????
?????. ???????
??????????????????
????TDD??Test-Driven Development???????????????????????????
????BDD??Behavior Driven Development??????????????????????????
????????TDD??BDD?????????? ????TDD??BDD??DDD???Щ???? ??
???????????????????? ???????????????????? 5 ?????? ??
????mocha????????BDD??
??????. Mocha????
??????Node.js?У?????????е???????????? mocha + chai ??mocha????????????chai????????????????”???”??
??????????????У? jasmine ?? should.js ?? chai ?? assert ??????????????????????????????????chai??????assert?????????????????????
????Mocha????????У?
??????????????????????Promise??
??????????????coverage??????棻
??????????ò????????????????
????… …
??????????????? Mocha + chai(assert???) ??
??????????£?
????npm install mocha -g
????npm install mocha
????npm install chai
??????Mocha?????????
????mocha [debug] [options] [files]
?????磺
????mocha --recursive test/
??????. Mocha????????????????
????Mocha????????????????
????describe(moduleName?? function)
????describe????????????????????????????
????describe('????????????'?? function() {
????// ....
????});
????it(info?? function)
????info???????????????it???????????????????
????describe('?????????????????д???????????'?? function() {
????// ....
????});
????assert.equal(exp1?? exp2)
????mocha?????????ж?exp1??????exp2??
??????. Mocha????????????
????done
???????it??????????done??
????done??????????????????????????????
???????????mocha v3.x?汾??Promise??????????? done ??????????????????Promise?????????done??????
describe('User'?? function() {
describe('#save()'?? function() {
it('should save without error'?? function(done) {
var user = new User('Luna');
user.save(done);
});
});
});
??????. Test Hooks??????
????before() ?? after() ?? beforeEach() ?? afterEach() ?????BDD???????????????????test???????
????Test Hooks?????????????
????beforeEach?????describe?μ???????case??Ч??
????before??after??????????????????
????????describe?μ????????before??beforeEach??afterEach??after??
?????????it?ж??before????????????????Χ??describe??before?????????????
describe('hooks'?? function() {
before(function() {
// runs before all tests in this block
});
after(function() {
// runs after all tests in this block
});
beforeEach(function() {
// runs before each test in this block
});
afterEach(function() {
// runs after each test in this block
});
// test cases
});
????Hooks??????д????
????beforeEach(function() {
????});
????beforeEach(function nameFun() {
????});
????beforeEach("some description"?? function() {
????});