Angular Jasmine:单元测试监视嵌套函数的调用
我正在运行angular应用程序的单元测试,我监视func1是否调用了它?但在func1中,我调用func2,我想知道func2是否被调用。如何构造我的单元测试?我正在使用jasmine 这是我试过的 组件规格tsAngular Jasmine:单元测试监视嵌套函数的调用,angular,typescript,unit-testing,jasmine,Angular,Typescript,Unit Testing,Jasmine,我正在运行angular应用程序的单元测试,我监视func1是否调用了它?但在func1中,我调用func2,我想知道func2是否被调用。如何构造我的单元测试?我正在使用jasmine 这是我试过的 组件规格ts let spyOnCreateEntitlement = spyOn(component,"func1").and.callThrough(); let spyOnSetRecord = spyOn(component,"func2").and.callThrough(); fixt
let spyOnCreateEntitlement = spyOn(component,"func1").and.callThrough();
let spyOnSetRecord = spyOn(component,"func2").and.callThrough();
fixture.detectChanges();
expect(func1).toHaveBeenCalled();//passes
expect(func2).toHaveBeenCalled();//fails!!!!
func1(){
func2();
}
func2(){
...some logic...
}
组件。ts
let spyOnCreateEntitlement = spyOn(component,"func1").and.callThrough();
let spyOnSetRecord = spyOn(component,"func2").and.callThrough();
fixture.detectChanges();
expect(func1).toHaveBeenCalled();//passes
expect(func2).toHaveBeenCalled();//fails!!!!
func1(){
func2();
}
func2(){
...some logic...
}
我试图复制:
组件技术
testFunction1() {
this.testFunction2();
}
testFunction2() {
}
规格:
it('should call functions', () => {
let spyOnCreateEntitlement = spyOn(component, 'testFunction1').and.callThrough();
let spyOnSetRecord = spyOn(component, 'testFunction2').and.callThrough();
component.testFunction1();
expect(spyOnCreateEntitlement).toHaveBeenCalled();
expect(spyOnSetRecord).toHaveBeenCalled();
});
=>测试通过。
我猜原因是您有“expect(func1).tohavebeencall()”,您应该在这里有您的间谍名称或component.func1您能分享更多component.ts的代码吗?它可能包含需要在单元测试中进行一些同步的异步内容;在我的例子中,也许你可以提供更多关于你有什么样的函数的细节,它们有异步代码吗?您如何调用第一个函数,因为在您的测试中,您不执行它,只创建了spy