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

问题描述 投票:0回答:1

我正在尝试编写一个单元测试,该单元测试应该在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();
        });
    });
  });
});

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

javascript mongoose mocha chai sinon
1个回答
0
投票

[如果有人遇到了这个(很可能是将来的我)。

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

MyModelMock.expects('find').chain('where').chain('in').chain('exec').resolves(set);
© www.soinside.com 2019 - 2024. All rights reserved.