router.navigate test TypeError:无法读取未定义的属性“ root”

时间:2019-02-21 10:48:38

标签: angular unit-testing routing karma-jasmine

我正在尝试为内部包含 router.navigate 的方法创建简单的测试,不幸的是,我认为我应该在导入文件中包含RouterTestingModule。因此,我正在导入RouterTestingModule,并且在提供程序中,我也有Router来监视导航。有了这个我得到这个错误,可能是由于覆盖路由器。

组件中的方法

handleClickOpen={...}

规格文件为:

IconButton

有人可以给我提示这个问题吗? 当我删除RouterTestingModule时,我得到

onUserInfo() {
    this.router.navigate(['student/info']);
  }

当我删除{提供:路由器,useValue:mockRouter}时,我会得到

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']);

    }))

有什么解决方法吗?预先感谢。

编辑(解决方案)

'router-outlet' is not a known element:

0 个答案:

没有答案