我正在为一个对话框组件编写测试,该组件处理调用函数时手动提示的 PWA 安装事件。然后它将解决从BeforeInstallPromptEvent.userChoice
属性返回的承诺。
但是,当我尝试测试此功能时,我从 jasmine 收到错误消息:
'userChoice.then 不是函数'
我理解这一点,因为我尝试使用 spyOn 对其进行监视,然后检查结果。
我用 Promises 尝试了其他一些事情,因为 userChoice 返回了一个已解决的承诺(https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent),但到目前为止没有任何效果。
这是我要测试的功能。BeforeInstallPromptEvent
存储在 installEvent 对象中,该对象从 传递到组件,app.ts
然后存储在数据中。
this.data.installEvent.userChoice
.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
doSomething();
在我的测试规范中,我使用以下内容创建了一个 mockData 对象:
const mockData = {
installEvent: {
prompt: () => {},
userChoice: Promise.resolve().then(() => {
return { choiceResult: { outcome: "accepted" } };
}),
},
};
到目前为止,我的测试规范(经过多次不同的尝试)是这样的:
it("should do something after user accepted", fakeAsync(() => {
mockData.installEvent.userChoice.then((result) => {
return result.choiceResult.outcome;
//this test returns true
expect(result.choiceResult.outcome).toEqual("accepted");
});
flush();
//but the function gets never called
expect(component.doSomething).toHaveBeenCalled();
});
或者使用产生错误的spyOn
函数:userChoice
it("should do something after user accepted", () => {
spyOn(mockData.installEvent, "userChoice").and.returnValue("accepted");
expect(mockData.installEvent.userChoice).toBe("accepted)
expect(component.doSomething).toHaveBeenCalled();
});
有了这个,我设法已经测试了该data.installEvent.prompt
函数是否被调用。
但我不知道如何测试userChoice
Promise 解析为“已接受”,然后触发 if 条件。
请帮我。由于我是一个关于测试(在 Angular 中)的初学者,因此很高兴能够解释测试此类 Promises 背后的方法。
非常感谢您!