【发布时间】:2021-12-10 06:56:18
【问题描述】:
由于某种原因,我无法测试拒绝。这是功能,我想 100% 测试它。现在它像 92% 并且抱怨拒绝(e)没有测试..
public resolve(): Promise<{ [key: string]: boolean }> {
return new Promise<{ [key: string]: boolean }>(async (resolve, reject) => {
try {
for await (const setting of settings) {
// ObjectStore that acts like hashMap
this.store.set(setting.key, setting.value);
}
resolve();
}
catch (e) {
reject(e);
}
});
}
更新:
我必须制作另一个 Mock 才能使 catch 语句首先发生。 而且 TestBed.overrideProvider 并没有真正为我工作,所以我不得不
it('should return a rejected promise', async() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
//inserting the new mock provider to trigger the catch
...
});
然后使用下面的答案(谢谢@Apoorva Chikara)并为我工作
如果有更简单的方法,请告诉我
这是我更新的测试代码。
it('should return a rejected promise', async() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [
{
provide: ObjectStore,
useValue: new ObjectStore('test'),
},
{
// Making this client invalid to trigger catch and throw error
// I think writing a mock class to throw error also can be done
// this is the "settings" from azure listConfigurationSettings
provide: AppConfigurationClient,
useValue: {},
}
]
});
service = TestBed.inject(FeatureFlagResolver);
store = TestBed.inject(ObjectStore);
const rejection = async () => {
await service.resolve().catch();
}
await expect(rejection()).rejects.toThrow();
});
“设置”是来自 MS azure 的 listConfigurationSettings。我们曾经从 azure 读取配置设置。 例如,我们想在 Angular 站点中启用某些东西,我们可以去 azure 门户更改应用程序设置(“聊天机器人”,“开”),这样聊天机器人就会出现在页面上。只是人们要打开、关闭的应用设置。
抱歉,我应该首先提供更多信息。
【问题讨论】:
-
什么是
settings? -
在主线程中回复了上面
标签: javascript angular unit-testing jestjs karma-jasmine