TypeError:存根期望产生,但是没有通过单元测试返回回调

时间:2019-11-08 11:09:43

标签: javascript mongoose mocha chai sinon

我正在尝试编写一个单元测试,该单元测试应该在REST端点和属于它的控制器之间执行集成测试。测试应模拟对数据库的调用,以便在测试期间不建立数据库连接。

我正在使用chai-http进行对端点的HTTP调用,并使用sinon-mongoose进行sinon来模拟Mongoose模型调用。

const set = [{ _id: 1 }, { _id: 2 }, { _id: 3 }];

//Require the dev-dependencies
const sinon = require('sinon');
const { describe, it } = require('mocha');
require('sinon-mongoose');
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../src/server');

const should = chai.should();

// set up mocks
const MyModel = require('../src/models/myModel');

const MyModelMock = sinon.mock(MyModel);
MyModelMock.expects('find').yields(set);

chai.use(chaiHttp);

describe('My endpoints', () => {
  describe('/GET to my endpoint', () => {
    it('it should GET all the info I want', (done) => {
      chai.request(server)
        .get('/api/myEndpoint')
        .end((err, res) => {
          res.should.have.status(200);
          done();
        });
    });
  });
});

谷歌搜索此错误并没有产生任何我可以使用的结果。我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

万一有人碰到这个(很可能是将来的我)。

我设法解决了我的问题。我在代码中使用了promises,应该已经相应地设置了我的模拟(也可以正确链接)。

MyModelMock.expects('find').chain('where').chain('in').chain('exec').resolves(set);