【发布时间】:2021-03-09 04:09:36
【问题描述】:
我正在学习用 jasmine 编写测试用例,我正在尝试创建一个测试用例来检查函数中定义的函数是否被调用
我要测试的功能如下,sData方法写在另一个组件中,当前组件正在扩展
public rPage() {
this.sData();
this.setupPage()
}
我写的测试用例如下
it('should check if sData is called', () => {
const sData = spyOn<any>(component, 'sData');
component.rPage();
fixture.detectChanges();
expect(sData).toHaveBeenCalled();
});
已经在 beforeeach 中的 rpage 上创建了 spy,如下所示
beforeEach(() => {
fixture = TestBed.createComponent(BasicComponent);
component = fixture.componentInstance;
spyOn(component, 'rPage');
fixture.detectChanges();
});
当我运行测试时,测试用例失败说“预期的间谍 sData 已被调用。” ,我哪里出错了
【问题讨论】:
标签: angular jasmine karma-jasmine