Failed: Cannot read property 'queryParams' of null at
I'm assuming it's because I have the following in ngOnInit():
ngOnInit() {
this.route.queryParams.subscribe(async params => {
this.userInfo = await JSON.parse(params['user_info']);
});
So far I've tried constructing my unit test with the following:
describe('AddItineraryPage', () => {
let component: AddItineraryPage;
let fixture: ComponentFixture<AddItineraryPage>;
let routeStub;
beforeEach(async(() => {
routeStub = null;
TestBed.configureTestingModule({
declarations: [ AddItineraryPage ],
imports: [IonicModule.forRoot(), FormsModule, ReactiveFormsModule, RouterTestingModule],
providers: [
{provide: ActivatedRoute, useValue: routeStub}
]
}).compileComponents();
fixture = TestBed.createComponent(AddItineraryPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
routeStub.queryParams = {
displayName: '',
dateOfBirth: '',
email: '',
photos: [],
location: '',
bio: '',
intDestination: [],
userId: ''};
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component).toBeTruthy();
});
});
});
So when the property
queryParamsis being called on therouteStubobject, it is null. You initialized therouteStubto null, so it makes sense.ngOnInit()gets called the first time you callfixture.detectChanges(), so you need to assign something to therouteStubbefore you make that call.Also in your code, you call
subscribe()on thequeryParams, so you will need to assign an Observable-like object to the property. You can just use an actualObservableby usingObservable.of().So your test code should look more like