html在带有索引的Angular 测试中选择元素值

yr9zkbsy  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(115)

我正试图为angular中的动态选择元素编写一个单元测试,如下所示。但不知何故,我从select和index中获得了值,而不仅仅是文本。
我的代码:

type= "x";
  temp: any;
  ngOnInit() {
    this.temp =
      this.type === 'x' ? ['one', 'two'] : ['three', 'two'];
  }
<select id="type" 
  (change)="change($event)">
  <option *ngFor="let lt of temp" [value]="lt">
    {{ lt }}
  </option>
</select>

我的测试

describe("test", () => {
    it("type", () => {
      fixture.detectChanges();
      const select: HTMLSelectElement = fixture.debugElement.query(By.css("select#type")).nativeElement;
      expect(select.options[0].value).toEqual("one");
    });
  });

当我以Angular 运行测试时,会出现以下错误: Error: Expected '0: one' to equal 'one'. 知道为什么会这样吗?我知道我可以通过将其更改为“0:1”来解决此问题。但是,我想知道我做错了什么。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题