求推荐Node.js 中为 Restful API 编写单元测试的例子
发布于 7 年前 作者 wang-weifeng 4038 次浏览 来自 问答

Node.js 中为 Restful API 编写单元测试

有没有好的这方面的例子求推荐,谢谢

7 回复

用这个库包。 restspec 具体的写法参考这个。绝对简单 https://github.com/open-node/open-rest-es6-boilerplate

有问题随时问我。

@stonephp 额 有其他的吗 相对比对比

@rwing 其他的你搜索测试相关的东西应该能找到很多,写起来都比我这个复杂很多。

for example:

/* jslint node: true */
'user strict';
var chai = require('chai');
var app = require('../server');
var request = require('supertest');

chai.should();

var postData = {
  appId: 1,
  stamp: 20151010172847,
  userId: 11078,
  proType: 1,
  data: '',
  sign: 1
};

describe("测试Base2Pro类的接口", function() {

  describe("#/v1/base2pro/data/transmit", function() {

    describe("天气推送 —— checkWeatherPush", function () {
      it("正常参数输入时,返回码为200", function(done) {
        request(app)
          .post('/v1/base2pro/data/transmit?lat=40.227932&lon=116.160439&handleType=CheckWeatherPush')
          .send(postData)
          .expect(200, done);
      });
    });

    describe("水电量 —— getWasherCost", function () {
      it("正常参数输入时,返回码为200", function(done) {
        request(app)
          .post('/v1/base2pro/data/transmit?handleType=GetWasherCost&time=20151023&applianceId=17592195755496&resultType=2&expendType=1')
          .send(postData)
          .expect(200, done);
      });
    });

  });

});

https://github.com/avajs/ava

import test from 'ava';

test(t => {
    t.deepEqual([1, 2], [1, 2]);
});
回到顶部