测试用例
现在,我们创建一个名字为 tests 的测试文件夹,并创建一个index.js的文件。测试用例前需要启动服务器,结束后关闭服务。这个时候就用到了前面暴露的 boot() 和 shutdown() 方法。
示例如下:
var boot = require('../app').boot,
shutdown = require('../app').shutdown,
request = require('superagent'),
expect = require('chai').expect;
describe('server', function () {
before(function () {
boot();
});
describe('index', function () {
it('should respond to GET', function (done) {
request
.get('http://localhost:3000')
.end(function (err, res) {
expect(res.status).to.equal(200);
done();
});
});
});
after(function () {
shutdown();
});
});
运行命令:mocha tests
- 在tests文件夹中创建一个index.js文件
- 再把上述中的内容写入到index.js文件中,然后点击保存文件。一定要保存!一定要保存!一定要保存!重要的事说要三遍(不建议拷贝)
- 最后在linux环境中输入命令:mocha tests。