简体   繁体   English

使用 karma jasmine 使用路由器 navigateByUrl 对 function 进行单元测试

[英]unit test a function with router navigateByUrl using karma jasmine

I am trying to unit test the following function which has router navigateByUrl, but couldnt execute.then block我正在尝试对以下具有路由器 navigateByUrl 的 function 进行单元测试,但无法执行。然后阻止

.ts code: .ts 代码:

back(){ 

    this.router.navigateByUrl('/routepath').then(() => {

         Object.keys(['column']).forEach((obj, val) => {
            myService.setDefaultGroup();
         });

    });
}

Here is what i have tried in my spec.ts这是我在我的 spec.ts 中尝试过的

class MockRouter {
   navigateByUrl = () => {
    return {
      then: () => {}
     }
   }
}

configureTestSuite(() => {
    TestBed.configureTestingModule({
       providers: [{ provide: Router, useClass: MockRouter } ]
    })
});

describe('should call back', () => {
    it('should call Router.navigateByUrl', inject([Router], (router: Router) => {
        const spy = spyOn(router, 'navigateByUrl');
        component.back();
    }));
});

What is the output received收到的output是什么

TypeError: Cannot read property 'then' of undefined类型错误:无法读取未定义的属性“then”

Expected:预期的:

Test case should execute测试用例应该执行

I am using karma and jasmine to unit test this.我正在使用业力和 jasmine 对此进行单元测试。 Help me out if i am missing anything如果我遗漏了什么,请帮助我

When you spyOn a method, it stubs it out to return undefined.当您spyOn一个方法时,它会将其存根以返回未定义。

You need to use .and.callThrough() to not lose implementation details.您需要使用.and.callThrough()以免丢失实现细节。

Try:尝试:

describe('should call back', () => {
    it('should call Router.navigateByUrl', inject([Router], (router: Router) => {
        const spy = spyOn(router, 'navigateByUrl').and.callThrough();
        component.back();
    }));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Karma / Jasmine for Router 的 Angular 单元测试错误 - Unit Test error for Angular using Karma / Jasmine for Router 如何使用 Karma 和 Jasmine 在 Angular 中模拟 router.navigateByUrl(...).then - How to mock router.navigateByUrl(…).then in Angular with Karma and Jasmine 带有 jasmine karma 的 Angular 单元测试错误(finalSprintsList.map 不是函数) - Angular unit test error ( finalSprintsList.map is not a function ) with jasmine karma 如何对函数的返回值进行单元测试 - Angular (Jasmine/Karma) - How to unit test return value of function - Angular (Jasmine/Karma) Angular4-如何使用Karma和Jasmine对指令进行单元测试 - Angular4 - how to unit test a directive using Karma and Jasmine 如何使用 Jasmine /Karma 在 Angular 单元测试中测试 If Else 块 - How to test If Else block in Angular Unit Testing using Jasmine /Karma 使用 karma 和 jasmine 以 .find 方法覆盖数组的单元测试 - Covering unit test for array with .find method in angular using karma and jasmine Angular 中的 Mock Base 服务 使用 Jasmine 和 Karma 进行单元测试 - Mock Base service in Angular Unit test using Jasmine and Karma 无法使用Karma和Jasmine进行Angular应用程序的单元测试 - Unable to Unit test Angular application using Karma and Jasmine 如何使用angular 7 karma / jasmine对@hostlistener粘贴事件进行单元测试 - How to unit test @hostlistener paste event using angular 7 karma/jasmine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM