简体   繁体   English

路由器事件未在单元测试中执行

[英]Router events not executing in unit test

I am trying to test my router.events , but the events are not triggering, and I am not sure why.我正在尝试测试我的router.events ,但事件没有触发,我不知道为什么。 I have tried this with no luck.我试过这个没有运气。

Here is how I have my test setup:这是我的测试设置:

describe('AppComponent', () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let router: Router;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule, 
        RouterTestingModule.withRoutes([
          { path: Constants.routes.login, component: LoginComponent },
          { path: Constants.routes.admin.admin, component: AdminWelcomeComponent },
          { path: Constants.routes.user.user, component: HomeComponent }
        ])
      ]
      schemas: [NO_ERRORS_SCHEMA],
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    router = TestBed.inject(Router);
  });


  it('should be created', () => {
    component.apps = [{ name: '', url: '/test' }];
    component.ngOnInit();

    const event = new NavigationEnd(42, '/test', '/test');
    (TestBed.inject(Router).events as Subject<Event>).next(event);
  });
});

As seen in this screenshot, the subscription is not executed.如此屏幕截图所示,未执行订阅。

漏线

You also have to make sure that change detection is run:您还必须确保运行更改检测:

/* ... */
fixture.detectChanges();

(TestBed.inject(Router).events as Subject<Event>).next(event);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM