Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Jasmine:单元测试监视嵌套函数的调用_Angular_Typescript_Unit Testing_Jasmine - Fatal编程技术网

Angular Jasmine:单元测试监视嵌套函数的调用

Angular 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

我正在运行angular应用程序的单元测试,我监视func1是否调用了它?但在func1中,我调用func2,我想知道func2是否被调用。如何构造我的单元测试?我正在使用jasmine

这是我试过的

组件规格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...
}
组件。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