使用 MatSort 在 Angular 中进行单元测试排序

编程入门 行业动态 更新时间:2024-10-05 11:17:08

使用 MatSort 在 Angular 中进行<a href=https://www.elefans.com/category/jswz/34/1771349.html style=单元测试排序"/>

使用 MatSort 在 Angular 中进行单元测试排序

我目前在尝试测试 Angular 项目中的排序功能时遇到问题。 我不明白为什么在浏览 HTML 时我无法获得任何“表格”元素。 我正在尝试使用 MatSort 测试排序。

.html 的片段:

<div class="table-wrapper">
  <div class="loader-shade" *ngIf="isLoading">
    <mat-spinner *ngIf="isLoading"></mat-spinner>
  </div>
  <table mat-table="mat-table" #table="" [dataSource]="dataSource" matSort="matSort">
    <ng-container matColumnDef="column1">
      <th mat-header-cell="" *matHeaderCellDef="" mat-sort-header="">COLUMN1</th>
      <td mat-cell="mat-cell" *matCellDef=""></td>
    </ng-container>
    <ng-container matColumnDef="column2">
      <th mat-header-cell="" *matHeaderCellDef="" mat-sort-header="">COLUMN2</th>
      <td mat-cell="mat-cell" *matCellDef=""></td>
    </ng-container>
  </table>
</div>

.spec.ts 文件的片段:

  it('should sort by row clicked', () => {
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    const table = compiled.querySelector('table');
    console.log('Table : ' + table);
  });

当我运行当前测试类时,日志返回一个空的 HTMLTableElement,因此我无法继续检索我正在寻找的

th
以测试排序。

任何建议或帮助将不胜感激!

我已经尝试过这里的建议:How to test MatSort with Jasmine 但它没有帮助。

我也尝试遵循这样的事情:ponent.spec.ts 但它也没有帮助.

回答如下:

更多推荐

使用 MatSort 在 Angular 中进行单元测试排序

本文发布于:2024-05-30 07:27:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   MatSort   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!