Stub 没有 return 值
Stub doesn't return a value
我使用 Mocha 作为测试运行程序,使用 Chai 进行断言,使用 Sinon。
我在使用 sinon 时遇到问题,我想在 PrestigeQuoteService.js 文件
中测试以下功能
find: function (criteria) {
return PrestigeQuote.find(criteria)
.populate('client')
.populate('vehicle')
.populate('quoteLogs');
},
这是我的测试用例
describe('find()', function () {
describe('prestige quote found', function () {
before(function () {
sandbox = sinon.sandbox.create();
var mockChain = {
populate: function () {
return this;
}
};
sandbox
.stub(PrestigeQuote, 'find').returns(mockChain);
});
it('should return a particular quote', function (done) {
PrestigeQuoteService.find({id: 1}, function (err, result) {
result.should.exist;
done();
});
});
after(function () {
sandbox.restore();
});
});
});
但我还是收到了这个错误,即使我认为我已经完成了()并且默认应该 return 值。
Error: timeout of 10000ms exceeded. Ensure the done() callback is being called in this test.
在 it() 函数中使用超时。
describe('find()', function () {
describe('prestige quote found', function () {
before(function () {
sandbox = sinon.sandbox.create();
var mockChain = {
populate: function () {
return this;
}
};
sandbox
.stub(PrestigeQuote, 'find').returns(mockChain);
});
it('should return a particular quote', function (done) {
this.timeout(50000);
PrestigeQuoteService.find({id: 1}, function (err, result) {
result.should.exist;
done();
});
});
after(function () {
sandbox.restore();
});
});
});
我通过在 mockChain
中添加一个 return 来解决它
describe('prestige quote found', function () {
before(function () {
sandbox = sinon.sandbox.create();
var err = null;
var mockChain = {
populate: function () {
return this;
},
return:function () {
return {};
}
};
sandbox
.stub(PrestigeQuote, 'find').returns(mockChain);
});
it('should return a particular prestige quote', function (done) {
PrestigeQuoteService.find({id: 1}, function (result) {
result.should.exist;
});
done();
});
after(function () {
sandbox.restore();
});
});
我使用 Mocha 作为测试运行程序,使用 Chai 进行断言,使用 Sinon。 我在使用 sinon 时遇到问题,我想在 PrestigeQuoteService.js 文件
中测试以下功能 find: function (criteria) {
return PrestigeQuote.find(criteria)
.populate('client')
.populate('vehicle')
.populate('quoteLogs');
},
这是我的测试用例
describe('find()', function () {
describe('prestige quote found', function () {
before(function () {
sandbox = sinon.sandbox.create();
var mockChain = {
populate: function () {
return this;
}
};
sandbox
.stub(PrestigeQuote, 'find').returns(mockChain);
});
it('should return a particular quote', function (done) {
PrestigeQuoteService.find({id: 1}, function (err, result) {
result.should.exist;
done();
});
});
after(function () {
sandbox.restore();
});
});
});
但我还是收到了这个错误,即使我认为我已经完成了()并且默认应该 return 值。
Error: timeout of 10000ms exceeded. Ensure the done() callback is being called in this test.
在 it() 函数中使用超时。
describe('find()', function () {
describe('prestige quote found', function () {
before(function () {
sandbox = sinon.sandbox.create();
var mockChain = {
populate: function () {
return this;
}
};
sandbox
.stub(PrestigeQuote, 'find').returns(mockChain);
});
it('should return a particular quote', function (done) {
this.timeout(50000);
PrestigeQuoteService.find({id: 1}, function (err, result) {
result.should.exist;
done();
});
});
after(function () {
sandbox.restore();
});
});
});
我通过在 mockChain
中添加一个 return 来解决它 describe('prestige quote found', function () {
before(function () {
sandbox = sinon.sandbox.create();
var err = null;
var mockChain = {
populate: function () {
return this;
},
return:function () {
return {};
}
};
sandbox
.stub(PrestigeQuote, 'find').returns(mockChain);
});
it('should return a particular prestige quote', function (done) {
PrestigeQuoteService.find({id: 1}, function (result) {
result.should.exist;
});
done();
});
after(function () {
sandbox.restore();
});
});