最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何测试expressJS控制器

IT培训 admin 3浏览 0评论

如何测试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这将给您一个很好的介绍。

也尝试茉莉花

如何测试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. 暂无评论