TypeError:存根应产生,但没有通过单元测试返回的回调



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

我使用chai-http对端点进行http调用,并使用sinon-mongoose模拟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();
});
});
});
});

在谷歌上搜索这个错误并没有产生任何我能够处理的结果。我在这里做错了什么?

万一有人遇到这种情况(很可能是未来的我(。

我设法解决了我的问题。我在代码中使用了promise,应该相应地设置mock(也正确地链接(。

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

最新更新