Angular 无法使用RouterTestingModule测试路由

Angular 无法使用RouterTestingModule测试路由,angular,angular6,angular-routing,angular-test,Angular,Angular6,Angular Routing,Angular Test,我有一个使用路由的组件。我想对路由进行单元测试,但无法使用RouterTestingModule进行单元测试 我写的spec是 import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; .... fdescribe('HomepageContentComponentComponent', () => { let component: HomepageContent

我有一个使用
路由的组件。我想对路由进行单元测试,但无法使用
RouterTestingModule
进行单元测试

我写的
spec

import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
....


fdescribe('HomepageContentComponentComponent', () => {
  let component: HomepageContentComponentComponent;
  let fixture: ComponentFixture<HomepageContentComponentComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports:[
        RouterTestingModule.withRoutes([
          {
            path: 'new-practice-question',
            component: NewPracticeQuestionComponent
          }]),
        ReactiveFormsModule,
        HttpClientTestingModule
      ],
      declarations: [ ...
      ],
      providers:[
        {provide: Location, useClass: SpyLocation},
        {provide: LocationStrategy, useClass: MockLocationStrategy},
        {provide: NgModuleFactoryLoader, useClass: SpyNgModuleFactoryLoader}
      ]
    })
    .compileComponents();
    fixture = TestBed.createComponent(HomepageContentComponentComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });
      it('should navigate to New Questions Component when New Question button is clicked',fakeAsync(()=>{
    let router:Router = TestBed.get(Router);
    let location:Location = TestBed.get(Location);
    console.log('initial router is ',router);
    console.log('initial location is ',location);
    //router.initialNavigation();
    router.navigate(['new-practice-question']).then(()=>{
      console.log('new router is ',router);
      console.log('new location is ',location);
      expect(location.path()).toBe('/new-practice-question');
    });


  }));
});
import{async,ComponentFixture,fakeAsync,TestBed,tick}来自“@angular/core/testing”;
....
fdescribe('HomepageContentComponentComponent',()=>{
let组件:HomepageContentComponentComponent;
let夹具:组件夹具;
在每个之前(()=>{
TestBed.configureTestingModule({
进口:[
RouterTestingModule.withRoutes([
{
路径:“新实践问题”,
组件:NewPracticeQuestionComponent
}]),
反应形式模块,
HttpClientTestingModule
],
声明:[。。。
],
供应商:[
{provide:Location,useClass:SpyLocation},
{提供:LocationStrategy,useClass:MockLocationStrategy},
{提供:NgModuleFactoryLoader,useClass:SpyNgModuleFactoryLoader}
]
})
.compileComponents();
fixture=TestBed.createComponent(HomepageContentComponentComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('单击新问题按钮时应导航到新问题组件',fakeAsync(()=>{
let router:router=TestBed.get(路由器);
let location:location=TestBed.get(location);
console.log('初始路由器是',路由器);
console.log('初始位置为',位置);
//router.initialNavigation();
路由器。导航(['new-practice-question'])。然后(()=>{
console.log('新路由器是',路由器);
console.log('新位置为',位置);
expect(location.path()).toBe(“/新练习问题”);
});
}));
});
我面临两个问题 1)
path()。因此,
expect
中的比较失败。
2) 我必须明确地为
SpyLocation
等提供
提供者
。为什么?我在网上看到的大多数示例似乎只使用了
RouterTestingModule.withRoots
,而不需要显式地提供
提供者。如果我不这样做,我会得到错误
没有位置提供程序

让它工作了。由于未知原因,选择了错误的
位置定义。当我从“@angular/common”添加
import{Location}时,测试成功了