由于某种原因我无法让我的测试工作并且它不断抛出错误:
Expected spy on isCurrentStatus to equal true.
被调用的函数只是评估传入的变量是否等于当前持有的 status
属性,并返回 true 或 false。没什么大不了的……
测试
it('should return true if current status = status passed in', () => {
const statusSpy = spyOn(component, 'isCurrentStatus');
component.event = failedEvent;
component.isCurrentStatus('failed');
expect(statusSpy).toEqual(true);
})
组件
event: MyEvent;
isCurrentStatus(status: string): boolean {
return this.event.status === status;
}
更新
我刚刚将 spyOn
移动到 beforeEach()
部分,现在返回:
expected
undefined
to equaltrue
最佳答案
您可以在该函数上创建一个 spyOn 并检查它返回的不同值:
spyOn(component, 'isCurrentStatus').and.callThrough();
component.event = failedEvent;
const statusResult = component.isCurrentStatus('failed');
expect(statusResult).toBeTruthy();
关于javascript - 无法让 spyOn 测试正常工作 - Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52151981/