Issue
I'm using this, from here:
constructor(private heroService: HeroService,
private activatedRoute: ActivatedRoute) {
}
ngOnInit() {
const heroId = this.activatedRoute.snapshot.paramMap.get('id');
this.heroService.getHeroById(heroId).subscribe((hero: Hero) => {
...
});
}
Now I have a unit test that needs to mock this ActivatedRoute. I've already check some answers here in stackoverflow but without luck.
Notice that I'm using snapshot.paramMap so many solutions do not work at all. For example, this, this, and this do not work.
I think the near valid answer could be:
{
provide: ActivatedRoute,
useValue: {paramMap: Observable.of(convertToParamMap({id: 1}))}
}
But does not work either.
The error showing is:
Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///HeroesModule/HeroDetailComponent_Host.ngfactory.js'.
error properties: Object({ INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2, HIERARCHY_REQUEST_ERR: 3, WRONG_DOCUMENT_ERR: 4, INVALID_CHARACTER_ERR: 5, NO_DATA_ALLOWED_ERR: 6, NO_MODIFICATION_ALLOWED_ERR: 7, NOT_FOUND_ERR: 8, NOT_SUPPORTED_ERR: 9, INUSE_ATTRIBUTE_ERR: 10, INVALID_STATE_ERR: 11, SYNTAX_ERR: 12, INVALID_MODIFICATION_ERR: 13, NAMESPACE_ERR: 14, INVALID_ACCESS_ERR: 15, VALIDATION_ERR: 16, TYPE_MISMATCH_ERR: 17, SECURITY_ERR: 18, NETWORK_ERR: 19, ABORT_ERR: 20, URL_MISMATCH_ERR: 21, QUOTA_EXCEEDED_ERR: 22, TIMEOUT_ERR: 23, INVALID_NODE_TYPE_ERR: 24, DATA_CLONE_ERR: 25, code: 19 })
This is because I'm doing a request that responses with status 500 because paramMap is undefined so there is no id on the request.
... any ideas?
Thanks and BR
Solution
Add snapshot to the useValue like:
{
provide: ActivatedRoute,
useValue: {
snapshot: {
paramMap: convertToParamMap({
id: '1'
})
}
}
}
Answered By - Ibrahim El_Khatib
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.