我有一个单元测试,正在其中测试getvalue
中的功能MainService
。此函数基于注入到MainService
中的服务返回的承诺返回值,我们将其称为InjectedService
。所以从本质上讲,我有一个类似于以下的功能。
public myFunction(provider: string, uuid: string, domain: string): string {
let url = "";
// TODO: Remove SDC logic 2-3 weeks after deployment
this.InjectedServerice.getValue()
.then((response: Response) => {
this.enabled = response.featureEnabled
})
.catch(() => {
this.enabled = false;
});
if(this.enabled) {
result = `SUCCESS`;
} else {
result = `FAIL`;;
}
return url;
}
问题在于,我所看到的关于模拟承诺的一切都表明我需要spyOn(InjectedPromise, 'getValue').and.returnValue(deferred.promise)
,但必须在调用该方法之后进行摘要。问题是我试图通过myFunction验证他的价值回报,所以我的测试逻辑最终看起来像
let result = MainService.myFunction(params, are, here);
$scope.$digest()
我观察到的是,当我在myFunction中记录值时,promise解析为undefined,但是看起来像在调用$ digest()之后,then块将使用预期的模拟promise值再次执行。但是未定义的结果已经保存到结果var中。
我观察到的所有示例最终都在$ digest()之后评估了$ scope变量,这是我试图做的吗?