最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何测试expressJS控制器

运维笔记admin12浏览0评论

如何测试expressJS控制器

如何测试expressJS控制器

试图对控制器进行单元测试,但是当我这样做时,出现以下错误。

我很乐意以不同的方式测试控制器。

错误:

TypeError:预期的sinon对象

 const test = require('sinon-test');



 describe('index (get all)', function() {
    beforeEach(function() {
      res = {
        json: sinon.spy(),
        status: sinon.stub().returns({ end: sinon.spy() })
      };
      expectedResult = [{}, {}, {}];
    });
    it(
      'should return array of vehicles or empty array',
      test(() => {
        this.stub(Vehicle, 'find').yields(null, expectedResult);
        Controller.index(req, res);
        sinon.assert.calledWith(Vehicle.find, {});
        sinon.assert.calledWith(res.json, sinon.match.array);
      })
    );
  });
回答如下:

尝试使用mocha测试框架和chai断言库。https://youtu.be/MLTRHc5dk6s这将给您一个很好的介绍。

也尝试茉莉花

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论