添加测试

  1. npm install --save-dev mocha
  2. npm install --save-dev chai
  3. npm install --save-dev sinon
  4. npm install --save-dev supertest
  5. npm install --save-dev zombie
  1. mkdir test
  2. touch test/test.js

将下面代码copy进去

  1. var request = require('supertest');
  2. var assert = require('chai').assert;
  3. var expect = require('chai').expect;
  4. require('chai').should();
  5. var app = require('../app');
  6. describe('GET /users', function(){
  7. it('respond with text', function(done){
  8. request(app)
  9. .get('/users/')
  10. .set('Accept', 'application/text')
  11. .expect('Content-Type', /text/)
  12. .expect(200, done);
  13. })
  14. })

执行测试

  1. mvc git:(master) mocha -u bdd
  2. ******************************************************
  3. MoaJS Apis Dump
  4. ******************************************************
  5. ┌─────────────────────────────────────────────────────┬────────┬────────┐
  6. File Method Path
  7. ├─────────────────────────────────────────────────────┼────────┼────────┤
  8. /Users/sang/workspace/stuq/aaaa/mvc/routes/index.js get /
  9. ├─────────────────────────────────────────────────────┼────────┼────────┤
  10. /Users/sang/workspace/stuq/aaaa/mvc/routes/test.js get /test
  11. ├─────────────────────────────────────────────────────┼────────┼────────┤
  12. /Users/sang/workspace/stuq/aaaa/mvc/routes/users.js get /users
  13. └─────────────────────────────────────────────────────┴────────┴────────┘
  14. GET /users
  15. GET /users/ 200 10.206 ms - 23
  16. respond with text (50ms)
  17. 1 passing (55ms)

就这样完成了简单的测试

测试生命周期是非常重要的,给出模板test2.js

  1. var request = require('supertest');
  2. var assert = require('chai').assert;
  3. var expect = require('chai').expect;
  4. require('chai').should();
  5. // 测试代码基本结构
  6. describe('UserModel', function(){
  7. before(function() {
  8. // runs before all tests in this block
  9. })
  10. after(function(){
  11. // runs after all tests in this block
  12. })
  13. beforeEach(function(){
  14. // runs before each test in this block
  15. })
  16. afterEach(function(){
  17. // runs after each test in this block
  18. })
  19. describe('#save()', function(){
  20. it('should return sang_test2 when user save', function(done){
  21. done()
  22. })
  23. })
  24. })

至此已有功能

  • livereload
  • mount-routes
  • test