Below is a simple component that I have. All necessary imports should be assumed:
//my-component.component.ts
//imports, decorator, etc.
routingNumber: number;
ngOnInit() {
this.route.params.subscribe(
params => {
this.routingNumber = +params['id'];
}
);
}
Additionally, there is a corresponding test file shown below. Once again, assume all relevant import statements are in place. A mockActivatedRoute is used instead of the actual ActivatedRoute, with an associated observable. The issue arises when calling fixture.onChanges(), as it seems that ngOnInit is not properly setting the routingNumber property value to the passed observable value - it always returns undefined during testing.
//my-component.component.spec.ts
//imports, describe, etc.
class mockActivatedRoute {
params = {
subscribe() {
return of({id: '1'});
}
}
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
NoteService,
{provide: ActivatedRoute, useClass: mockActivatedRoute},
{provide: DataStorageService, useValue: dataServiceStub}
],
declarations: [MyComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
it('should expect 1 as the route parameter', async(() => {
fixture.detectChanges();
expect(component.routingNumber).toEqual(1);
});