it('should raise selected event when clicked', () => {
let selectedHero: Hero;
comp.selected.subscribe((hero: Hero) => selectedHero = hero);
heroEl.triggerEventHandler('click', null);
expect(selectedHero).toBe(expectedHero);
});
不应该像
comp.selected.subscribe((hero: Hero) => {
selectedHero = hero;
expect(selectedHero).toBe(expectedHero);
});
答案 0 :(得分:1)
由于jasmine
而不能是这样。
如果你这样做:
it('should raise selected event when clicked', () => {
let selectedHero: Hero;
comp.selected.subscribe((hero: Hero) => {
selectedHero = hero;
expect(selectedHero).toBe(expectedHero);
});
heroEl.triggerEventHandler('click', null);
});
并且,由于存在错误,事件click
未被捕获,那么您的期望将永远不会被测试,并且您没有失败。
通过在selectedHero
之后测试click
,测试可确保如果未处理click事件,则测试将失败,因为selectedHero
将为undefined
。