Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?) in Angular RC 5 when unit testing

前端 未结 8 1373
闹比i
闹比i 2020-12-24 10:30

I just upgraded to Angular RC 5 and now all component that uses \'ROUTER_DIRECTIVES\' fails with \'Can\'t resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?)\' when I t

相关标签:
8条回答
  • 2020-12-24 11:24

    Was finally able to fix it, and it was as simple as this:

    beforeEach(() => addProviders([
        { 
            provide: Router, 
            useClass: class { navigate = jasmine.createSpy("navigate"); }
        }]));
    
    0 讨论(0)
  • 2020-12-24 11:27

    I did not have any luck with the above solutions. Instead, I followed the recommended approach in the official documentation: Testing Routed Components

    First, create a stubbed router with whatever methods your component calls:

    class RouterStub {
      navigateByUrl(url: string) {
        return url;
      }
    }
    

    Then, when configuring your testing module you do the following:

    TestBed.configureTestingModule({
      declarations: [HeaderComponent],
      providers: [
        {provide: Router, useClass: RouterStub}
      ]
    });
    
    0 讨论(0)
提交回复
热议问题