NODEJS WEB API接口测试

2018-4-21 Frank NodeJS

安装

npm install mocha -g
npm install should --save-dev
npm install supertest --save-dev
npm install benchmark --save-dev

example

'use strict';

let request = require('supertest');
let should = require('should');

describe('User', function () {
    let url = 'http://www.liangcuntu.com';
    let token = '';
    let open_id = '';
    before(function(done) {
        request(url).post('/api/users/login')
            .set('Accept', 'application/json')
            .send({
                'email': 'xxx@qq.com',
                'password': 'xxx',
            })
            .expect('Content-Type', /json/)
            .expect(200)
            .end(function(err, res) {
                if (err) done(err);
                res.body.should.have.property('open_id');
                res.body.open_id.should.equal('xxxx');
                token = res.body.access_token;
                open_id = res.body.open_id;
                done();
            });
      });
    it('User find', function (done) {
        request(url).get('/api/users/findByOpenId')
        .set('Accept', 'application/json')
        .set('authorization', 'Bearer ' + token)
        .send({
                'open_id': open_id
            })
            .expect('Content-Type', /json/)
            .expect(200)
            .end(function(err, res) {
                if (err) done(err);
                res.body.should.have.property('nickname');
                res.body.nickname.should.equal('frank');
                console.log(res.body.nickname);
                done();
            });
    });
});

运行

cd test
mocha test.js

参考
mocha

发表评论 登录

Top