我遇到单元测试的问题,测试使用sinon.js存根服务并返回一个promise,如下所示:
受测试的受控代码:
var CaseService = function() {
this.getCaseStatus = function() {
var deferred = Q.defer();
deferred.resolve({data: 'some data'});
return deferred.promise;
}
};
module.exports = CaseService;
单元测试
describe('Using the case service', function () {
var caseService;
before(function() {
caseService = new CaseService();
});
describe('making an API call', function() {
it('should be a success', function () {
sinon.stub(caseService, 'getCaseStatus').returns(Q.when({data: ['yo'] }));
caseService.getCaseStatus().then(function(result) {
// This doesn't work here as the test passes, it fails
// correctly if I were to move it up two lines.
expect(true).equal(false);
})
});
});
});
此时我收到了预期模拟数据的回复。但是,测试通过时显然不应该。如果我要搬家:
expect(true).equal(false);
向上两行,然后测试按预期失败。为什么“expect”在then()函数中不起作用?
任何帮助都将不胜感激。
干杯,
保