Angular router.navigate测试类型错误:无法读取属性';根';未定义的

Angular router.navigate测试类型错误:无法读取属性';根';未定义的,angular,unit-testing,routing,karma-jasmine,Angular,Unit Testing,Routing,Karma Jasmine,我正在尝试为包含路由器的方法创建简单的测试。在内部导航,不幸的是,我认为我的导入中应该有RouterTestingModule。正因为如此,我正在导入RouterTestingModule,在提供程序中,我还可以监视和导航路由器。因此,我得到了这个错误,这可能是因为覆盖路由器 组件中的方法 onUserInfo() { this.router.navigate(['student/info']); } 规范文件为: fdescribe('NavbarComponent', () =

我正在尝试为包含路由器的方法创建简单的测试。在内部导航,不幸的是,我认为我的导入中应该有RouterTestingModule。正因为如此,我正在导入RouterTestingModule,在提供程序中,我还可以监视和导航路由器。因此,我得到了这个错误,这可能是因为覆盖路由器

组件中的方法

onUserInfo() {
    this.router.navigate(['student/info']);
  }
规范文件为:

fdescribe('NavbarComponent', () => {
    let component: NavbarComponent;
    let fixture: ComponentFixture<NavbarComponent>;
    let appStore: Store<fromApp.State>
    const fakeUser = { username: '', password: '', role: 'PARENT' }
    let mockRouter;

    beforeEach(async(() => {
        mockRouter = {
            navigate: jasmine.createSpy('navigate')
          }

        TestBed.configureTestingModule({
            imports: [
                RouterTestingModule,
                SharedModule,
                AuthModule,
                MaterialModule,
                StoreModule.forRoot(reducers),
                EffectsModule.forRoot([AuthEffects, UserEffects])
            ],
            declarations: [
                AppComponent,
                WelcomeComponent,
                NavbarComponent,
                ProjectInfoComponent
            ],
            providers: [
                { provide: Router, useValue: mockRouter},

            ]
        }).compileComponents();

        appStore = TestBed.get(Store);
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(NavbarComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });
 it('test', fakeAsync(()=> {
        fixture.detectChanges();

        component.onUserInfo();
          expect(mockRouter.navigate).toHaveBeenCalledWith(['student/info']);

    }))
当我删除{provide:Router,useValue:mockRouter}时,我将得到

Expected spy navigate to have been called with [ [ 'student/info' ] ] but it was never called.
有什么解决办法吗?提前谢谢

编辑(解决方案)

fdescribe('NavbarComponent',()=>{
let组件:NavbarComponent;
let夹具:组件夹具;
让应用商店:商店
const fakeUser={用户名:“”,密码:“”,角色:'PARENT'}
让路由器:路由器
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[
RouterTestingModule.带路由(routes),
共享模块,
AuthModule,
材料模块,
StoreModule.forRoot(减速器),
EffectsModule.forRoot([AuthEffects,UserEffects])
],
声明:[
应用组件,
欢迎光临,
导航组件,
投影信息组件
],
供应商:[
]
}).compileComponents();
appStore=TestBed.get(Store);
路由器=测试台。获取(路由器)
}));
在每个之前(()=>{
fixture=TestBed.createComponent(NavbarComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('asd',fakeAsync(()=>{
fixture.detectChanges();
让navigateSpy=spyOn(路由器,'navigate');
onUserInfo();
期望(navigateSpy)。与(['student/info'])一起被调用;
}))
});
Expected spy navigate to have been called with [ [ 'student/info' ] ] but it was never called.
fdescribe('NavbarComponent', () => {
    let component: NavbarComponent;
    let fixture: ComponentFixture<NavbarComponent>;
    let appStore: Store<fromApp.State>
    const fakeUser = { username: '', password: '', role: 'PARENT' }
    let router: Router

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                RouterTestingModule.withRoutes(routes),
                SharedModule,
                AuthModule,
                MaterialModule,
                StoreModule.forRoot(reducers),
                EffectsModule.forRoot([AuthEffects, UserEffects])
            ],
            declarations: [
                AppComponent,
                WelcomeComponent,
                NavbarComponent,
                ProjectInfoComponent
            ],
            providers: [
            ]
        }).compileComponents();

        appStore = TestBed.get(Store);
        router = TestBed.get(Router)

    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(NavbarComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });


    it('asd', fakeAsync(()=> {
        fixture.detectChanges();

        let navigateSpy = spyOn(router, 'navigate');
        component.onUserInfo();
        expect(navigateSpy).toHaveBeenCalledWith(['student/info']);

    }))
});