我希望能够测试从一种视图传递到另一种视图的参数。我想测试一下参数是否存在,并且该参数是否与我提供的模拟测试数据匹配。
我是单元测试的新手,在阅读有关激活路线的测试以及传递参数方面做了大量的研究。我认为我坚持的是“期望”。它给我一个错误
Argument of type '{ urn: string[]; }' is not assignable to parameter of type 'Expected<Observable<Params>>'
零件
export class SearchComponent implements OnInit {
constructor(private route: ActivatedRoute, private router: Router) {
this.getParam();
}
ngOnInit() {
}
getParam():void {
this.route.queryParams.subscribe(params => {
console.log(params["urn"]);
});
}
}
规格
providers: [
HttpClient,
{
provide: ActivatedRoute,
useValue: {
queryParams: of({
urn: '123'
})
}
}
],
...
it('test queryParam in route', () => {
const activatedRoute: ActivatedRoute = fixture.debugElement.injector.get(ActivatedRoute);
activatedRoute.queryParams = of({ urn: '123' });
fixture.detectChanges();
// tick();
expect(activatedRoute.queryParams).toEqual({ urn: ['123'] }); // this line giving me trouble
});
如果有人可以帮我看看我在做什么错-这是我想到的stackBlitz演示
这里 :
expect(activatedRoute.queryParams).toEqual({ urn: ['123'] })
activatedRoute.queryParams
不是,{ urn: ['123'] }
而是Observable
会触发此值的。
您可以这样测试:
/*
Notice the extra "done" parameter. It is a function that you must call
to indicate that the test is finished.
It is necessary because we are testing an asynchronous method.
This will prevent the test from exiting until the "done" method is called.
Also, the test will fail if the done method is not called within 5s by default.
*/
it('test queryParam in route', (done) => {
[...]
activatedRoute.queryParams.subscribe((value) => {
expect(value).toEqual({ urn: ['123'] })
done();
})
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句