Ruu*_*oef 2 javascript mocha.js mongoose sinon chai
我正在尝试编写一个单元测试,它应该在 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();
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
谷歌搜索这个错误没有产生任何我能够使用的结果。我在这里做错了什么?
万一有人遇到这个(很可能是未来的我)。
我设法解决了我的问题。我在我的代码中使用了 Promise,应该相应地设置我的模拟(也正确链接)。
MyModelMock.expects('find').chain('where').chain('in').chain('exec').resolves(set);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1683 次 |
| 最近记录: |