使用茉莉花检查是否正在渲染多个元素

Checking if multiple elements are rendering using jasmine

it('should check if all div are appearing which are dependent on a common condition', () => {
  component.someCommonCondition = true;
  fixture.detectChanges();
  // now the prob lies in line below since it only gets the first element of the mentioned class
  const contentCheck = fixture.debugElement.query(By.css('.someClass'))
  expect() // what to write here to check all 3 divs are getting rendered
})
<div *ngif="someCommonCondition" class="someClass"></div>
<div *ngif="someCommonCondition" class="someClass"></div>
<div *ngif="someCommonCondition" class="someClass"></div>

您好,我正在尝试编写一个单元测试,我可以在其中测试是否正在呈现多个 div,这取决于一个共同的条件。我想知道是否有一种方法可以在一个测试中完成,而不是单独为每个测试编写测试,因为它们都依赖于一个共同的条件。也就是说,要么 none 个出现,要么全部出现。

我认为您可以使用 queryAll 而不是 queryqueryAll 将 return 一个数组。

尝试以下操作:

it('should check if all div are appearing which are dependent on a common condition', () => {
  component.someCommonCondition = true;
  fixture.detectChanges();
  // now the prob lies in line below since it only gets the first element of the mentioned class
  const contentChecks = fixture.debugElement.queryAll(By.css('.someClass'))
  expect(contentChecks.length).toBe(3);
})