Angular 2 Final Release Router Unit Test

后端 未结 3 1855
攒了一身酷
攒了一身酷 2020-12-01 07:49

How do I unit test routers in Angular version 2.0.0 with karma and jasmine?

Here\'s what my old unit test looks like in version 2.0.0-beta.14

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 08:33

    Instead of using useValue for routerStub, you can use useClass in the providers and it really worked for me.

    export class RouterStub {
      public url: string = '/';
      constructor() { }
        enter code here
      navigateByUrl(url: any) {
        this.url = url;
      }
    }
    

    And in the beforeEach just instantiate the routerStub object like

    routerStub = new RouterStub()    
    

    And in the test cases

    component.router.navigateByUrl('/test');
    fixture.detectChanges();
    

提交回复
热议问题