I have another way to mock a param or queryParam on ActivatedRoute.
To mock a param: 'http:://localhost/web/param'
beforeEach(async () => {
const params = new BehaviorSubject({ param: 'valueOf' });
const activatedRoute = { params, snapshot: { params } };
await TestBed.configureTestingModule({
declarations: [Component],
imports: [RouterTestingModule],
providers: [
someproviders...,
{
provide: ActivatedRoute, useValue: activatedRoute
}
]
})
.compileComponents();
});
this allow to change in distinct test on your file spec.ts the value of 'param'. To do this:
const params = new BehaviorSubject({ param: 'newValueOf' });
component['activeRoute'].params = params; //activeRoute is the public property in the constructor that instances ActivatedRoute
In this example, the url with the mock of 'param' is: 'http:://localhost/web/valueOf'
To queryParams: 'http:://localhost/web?queryParam=some'
do this:
beforeEach(async () => {
const queryParams = new BehaviorSubject({ queryParam: 'valueOf' });
const activatedRoute = { queryParams, snapshot: { queryParams: queryParams.value } };
await TestBed.configureTestingModule({
declarations: [ Component ],
providers: [
someproviders...,
{
provide: ActivatedRoute, useValue: activatedRoute
}
],
imports: [RouterTestingModule]
})
.compileComponents();
});
In this case, to set queryParams, you have to add .value.
paramMap
and notparams
. – Springer