我正在尝试使用 sinon.js 模拟 Sails 模型。当我测试我正在使用的部分来检索新创建的行时,我遇到了.fetch()
问题Model.create()
。
这是我要模拟的代码:
...
let newObject = await Object.create(newObjectData).fetch();
...
这是我的测试代码
const sinon = require('sinon');
const supertest = require('supertest');
describe('Object create action', function() {
let sandbox;
beforeEach(function() {
sandbox = sinon.createSandbox();
});
afterEach(function() {
sandbox.restore();
});
it('should create Object', function(done) {
const objectCreateStub = sandbox.stub(Object, 'create').callsFake(async function(data) {
console.log(data);
return {
key: 'value'
};
});
supertest(sails.hooks.http.app)
.post(`/objects`)
.send({
key: 'value'
})
.expect(200)
.end(done);
});
});
我不知道Object.create
伪造的函数应该返回什么.fetch
才能不抛出错误。所以,正如预期的那样,我收到了这个错误:
TypeError: Object.create(...).fetch is not a function
什么样的对象会Model.create()
返回,所以我也可以模拟它?是否有使用 Sails 和 Waterline 进行测试的最佳实践?
谢谢!