lintry

测试模块样例

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