在Angular Spectator测试中,within
方法用于在给定的选择器范围内查找元素。在一些情况下,可能需要使用等价的写法来实现相同的功能。
下面是within
方法的等价写法示例:
querySelectorAll
方法:const container = spectator.fixture.nativeElement;
const elementsWithinContainer = container.querySelectorAll('.some-selector');
Element.prototype.querySelectorAll
方法:const container = spectator.fixture.nativeElement;
const elementsWithinContainer = Element.prototype.querySelectorAll.call(container, '.some-selector');
Array.from
和querySelectorAll
方法:const container = spectator.fixture.nativeElement;
const elementsWithinContainer = Array.from(container.querySelectorAll('.some-selector'));
这些等价写法可以用来替代within
方法,以实现相同的查找元素的功能。请根据测试需求选择适合的写法。