Flux + Sinon + Promises 测试总是通过或超时
Flux + Sinon + Promises test always passes or times out
我目前正在尝试使用 Sinon + Sinon-As-Promised 测试 Flux,但我正在努力寻找相关示例,并且我的测试总是返回 true,无论断言如何。
我的测试目前是这样的:
it('Form store should contain encounter object', function() {
var stub = sinon.stub(restService, "loadForm").resolves(mockDataEncounter.Encounter);
stub().then(function(value){
console.log('Inside frmSrv');
formStore._currentForm = value;
expect(formStore._currentForm).to.have.property('Name');
})
console.log('calling action creator');
actionCreator.loadForm("123456789012345678910003");
})
我期待的是 -
Call action creator -> Action creator usually makes the API call,
but in this case calls the stubbed method instead -> Promise is
resolved, _currentForm is set to the mock data, and then the assertion
is checked.
然而实际情况是:
Call action creator -> Test passes -> Promise resolves
如果我向测试添加一个 done 回调,然后在断言之后调用 done,这会导致事情按我预期的顺序执行,但是如果我将预期的 属性 修改为 "Name1"(不存在),测试失败并出现超时错误,而不是说 属性 不存在的正确错误。
我是不是遗漏了一些基本的东西,或者只是以完全错误的方式处理事情?
因此,经过更多的摆弄后,我解决了这个问题,我不得不在承诺中添加一个 catch,然后调用 done 传递错误,以便显示正确的失败消息。
it('Form store should contain encounter object', function(done) {
var stub = sinon.stub(restService, "loadForm").resolves(mockDataEncounter.Encounter);
stub().then(function(value){
console.log('Inside frmSrv');
formStore._currentForm = value;
expect(formStore._currentForm).to.have.property('Name');
done();
}).catch(function(err){
done(err);
});
console.log('calling action creator');
actionCreator.loadForm("123456789012345678910003");
})
我目前正在尝试使用 Sinon + Sinon-As-Promised 测试 Flux,但我正在努力寻找相关示例,并且我的测试总是返回 true,无论断言如何。
我的测试目前是这样的:
it('Form store should contain encounter object', function() {
var stub = sinon.stub(restService, "loadForm").resolves(mockDataEncounter.Encounter);
stub().then(function(value){
console.log('Inside frmSrv');
formStore._currentForm = value;
expect(formStore._currentForm).to.have.property('Name');
})
console.log('calling action creator');
actionCreator.loadForm("123456789012345678910003");
})
我期待的是 -
Call action creator -> Action creator usually makes the API call, but in this case calls the stubbed method instead -> Promise is resolved, _currentForm is set to the mock data, and then the assertion is checked.
然而实际情况是:
Call action creator -> Test passes -> Promise resolves
如果我向测试添加一个 done 回调,然后在断言之后调用 done,这会导致事情按我预期的顺序执行,但是如果我将预期的 属性 修改为 "Name1"(不存在),测试失败并出现超时错误,而不是说 属性 不存在的正确错误。
我是不是遗漏了一些基本的东西,或者只是以完全错误的方式处理事情?
因此,经过更多的摆弄后,我解决了这个问题,我不得不在承诺中添加一个 catch,然后调用 done 传递错误,以便显示正确的失败消息。
it('Form store should contain encounter object', function(done) {
var stub = sinon.stub(restService, "loadForm").resolves(mockDataEncounter.Encounter);
stub().then(function(value){
console.log('Inside frmSrv');
formStore._currentForm = value;
expect(formStore._currentForm).to.have.property('Name');
done();
}).catch(function(err){
done(err);
});
console.log('calling action creator');
actionCreator.loadForm("123456789012345678910003");
})