I have a components that gets a value from the params property of a ActivatedRoute.
The components looks like:
......
constructor(public userRegistration: UserRegistrationService, public userLogin: UserLoginService,
public router: Router, public route: ActivatedRoute) {
}
ngOnInit() {
this.verificationCode = new FormControl('', [Validators.required]);
this.confirmationCodeForm = new FormGroup({verificationCode: this.verificationCode});
//******************************
this.sub = this.route.params.subscribe(params => {
this.email = params['userId'];
});
//******************************
this.errorMessage = null;
}
......
The test provides an ActivatedRoute as "useValue" that mocks the class. The test looks like:
describe('ConfirmRegistrationComponent', () => {
let component: ConfirmRegistrationComponent;
let fixture: ComponentFixture<ConfirmRegistrationComponent>;
let userRegistrationService: UserRegistrationService;
let userLoginService: UserLoginService;
let router: Router;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule, RouterTestingModule, HttpClientModule],
declarations: [ConfirmRegistrationComponent],
providers: [UserRegistrationService, UserLoginService, {
provide: ActivatedRoute,
useValue: {'params': Observable.from([{userId: 1}])}
}, ImageService, UserService, CognitoUtil,
{
provide: Router, useClass: class {
navigate = jasmine.createSpy('navigate');
}
}]
}).compileComponents();
fixture = TestBed.createComponent(ConfirmRegistrationComponent);
component = fixture.componentInstance;
userRegistrationService = TestBed.get(UserRegistrationService);
userLoginService = TestBed.get(UserLoginService);
spyOn(userLoginService, 'isAuthenticated').and.callFake(function () {
});
router = TestBed.get(Router);
fixture.detectChanges();
component.ngOnInit();
}));
it('should create', () => {
});
});
When I run the test, I get the next error:
Failed: Cannot read property 'subscribe' of undefined
TypeError: Cannot read property 'subscribe' of undefined
at new RouterLinkWithHref node_modules/@angular/router/esm5/router.js:6099:1)
Could you help me please? Thanks!