在Angular教程中,他们有一个HeroesComponent
的示例,其中包含子节点HeroesListComponent
。如何间谍在Angular中的子组件?
HeroesListComponent
使用HeroesService
至getHeroes()
。
为spyOn
getHeroes()
内heroes-list.component.spec.ts
,我们可以做
spyOn(fixture.debugElement.componentInstance.heroesService, 'getHeroes').and.returnValue(Promise.resolve([]))
我的问题是:如果我是HeroesComponent
规范的文件里 - heroes.component.spec.ts
我可以做这样的事情spyOn
孩子HeroesListComponent
getHeroes()
:
spyOn(fixture.debugElement.componentInstance.heroesListComponent.heroesService, 'getHeroes').and.returnValue(Promise.resolve([]))
很明显,这没有奏效,所以我问是否有人可以帮我解决问题。
我想这样做,因为我想测试在我打开HeroesComponent
时是否正确加载了<hero-list>
。这看起来可能是重复的,因为我已经进行了测试以确保HeroesListComponent
有效,但我认为这仍然是一个重要的测试,因为我确实想确保我的HeroesComponent
正确加载并且所有子组件都是。
我也试过嘲笑HeroesComponent
里面的整个HeroesService
,而不是使用spyOn
,但是我不能使这个语法工作。我不知道如何告诉HeroesListComponent
当我在里面时使用模拟物体heroes.component.spec.ts
谢谢!