【发布时间】:2016-09-22 11:42:05
【问题描述】:
我们刚刚为我们的测试库迁移到 mocha、chai 和 sinon,并且来自 jasmine,我对如何测试 Promise 有点困惑。
我有一个表单提交函数,它调用服务并在返回时将用户导航到正确的状态:
submit(event, theForm){
event.preventDefault();
if(theForm.$valid){
this.AuthenticationService.authenticateUser({
email: this.email,
password: this.password
}).then( (result) => {
this.$state.go('dashboard.home')
});
}
}
通过以下测试已经取得了部分进展:
it('should submit the login credentials if valid', function(){
var dfd = q.defer(),
promise = dfd.promise;
controller.email = 'ryan.pays@leotech.com.sg';
controller.password = 'Password123';
sinon.stub(service, 'authenticateUser').returns(promise);
sinon.spy(state, 'go');
sinon.spy(controller, 'submit');
controller.submit(event, theForm);
dfd.resolve();
expect(controller.submit).to.have.been.called;
expect(controller.submit).to.have.been.calledWith(event, theForm);
expect(event.preventDefault).to.have.been.called;
expect(service.authenticateUser).to.have.been.called;
expect(service.authenticateUser).to.have.been.calledWith({
email: controller.email,
password: controller.password
});
expect(state.go).to.have.been.called;
expect(state.go).to.have.been.calledWith('dashboard.home');
});
但是state.go 被调用的断言没有通过。我要对我的测试进行哪些更改才能使其通过?
【问题讨论】:
-
嘿@RyanP13 你找到解决上述问题的方法了吗?我也面临同样的问题。当试图测试代码“authenticateUser”promise..
标签: angularjs mocha.js karma-runner sinon chai