lintry

测试模块样例

{
"name": "demo-chai-http",
"version": "1.0.0",
"description": "",
"main": "test_template_by_chai-http.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@gitlab.kmlab.com:test/demo-chai-http.git"
},
"keywords": [],
"author": "lintry <shenlin00@gmail.com>",
"license": "MIT",
"dependencies": {
"chai": "^3.5.0",
"chai-http": "^3.0.0",
"should": "^11.1.1"
}
}
##使用chai-http的远程接口测试用例DEMO
### 安装运行
```sh
npm install
mocha test_template_by_chai-http.js
```
### 相关参考资料
- [mocha](https://github.com/mochajs/mocha)
- [chai](https://github.com/chaijs/chai)
- [chai-http](https://github.com/chaijs/chai-http)
- [should API](http://shouldjs.github.io/)
- [Mocha和Should、Supertest模块搭建Node.js单元测试环境](http://itbilu.com/nodejs/npm/VyrFOe51-.html)
- [测试用例:mocha,should,istanbul](http://wiki.jikexueyuan.com/project/node-lessons/mocha-should-istanbul.html)
describe('测试模块', function() {
var chai = require('chai'),
should = require('should'),
chaiHttp = require('chai-http');
chai.use(chaiHttp);
var agent = chai.request.agent('http://onwall.cn');
it('测试接口', function(done) {
// this.timeout(5000);
agent.post('/domino/b/auth_syslogin.do')
.send({user_code: 'IT001001DZ', user_password: '8ddcff3a80f4189ca1c9d4d902c3c909'})
.end(function(err, res) {
// 登录应该成功
try {
var result = JSON.parse(res.text);
} catch(e) {
console.error(e);
should.not.exist(e);
}
res.status.should.equal(200);
result.ret.should.equal('OK');
return agent.get('/domino/b/order_query.do')
.end(function(err, res) {
//返回值状态应该200且有OK
try {
var result = JSON.parse(res.text);
} catch(e) {
console.error(e);
should.not.exist(e);
}
res.status.should.equal(200);
result.ret.should.equal('OK');
//判断返回数据是否存在指定的属性或数值是否正确
let content = result.content;
content.should.have.property('order_status_map');
content.should.have.property('order_list');
content.order_list.length.should.aboveOrEqual(0);
done();
}
);
});
})
});