简体   繁体   English

自定义 Pipe 在运行单元测试时导致 ExpressionChangedAfterItHasBeenCheckedError

[英]Custom Pipe causes the ExpressionChangedAfterItHasBeenCheckedError when running a unit test

In my template I have this:在我的模板中,我有这个:

...
<tbody>
  <ng-container *ngFor="let item of items">
    <tr *ngIf="item.isBoolean">
      <td><a href="#">{{item.description}}</a></td>
      <td>{{item | customPipe}}</td>
    </tr>
  </ng-container>
</tbody>
...

And this is my unit test:这是我的单元测试:

it('should behave...', () => {
  const trs: DebugElement[] = fixture.debugElement.queryAll(By.css('tbody tr'));

  expect(trs.length).toBeGreaterThan(0);
});

When I execute the test, I get the following:当我执行测试时,我得到以下信息:

Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'value'. Current value: 'undefined'.. Find more at https://angular.io/errors/NG0100
error properties: Object({ code: '100' })
Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'value'. Current value: 'undefined'.. Find more at https://angular.io/errors/NG0100
    at throwErrorIfNoChangesMode (node_modules/@angular/core/fesm2015/core.js:6723:1)
    at bindingUpdated (node_modules/@angular/core/fesm2015/core.js:12854:1

That value is what the customPipe returns.valuecustomPipe返回的值。

I faced this issue today during testing my Angular app through Jasmine .我今天在通过Jasmine测试我的 Angular 应用程序时遇到了这个问题。 Please check the code snippet below which solved my problem (courtesy: https://www.codegrepper.com ).请检查下面解决我的问题的代码片段(礼貌: https://www.codegrepper.com )。

import { ChangeDetectorRef, AfterContentChecked } from '@angular/core';
constructor(private ref: ChangeDetectorRef){}

  ngAfterContentChecked() {
    this.ref.detectChanges();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为单元测试注入自定义 Pipe - Injecting Custom Pipe for Unit Test 使用异步 pipe 时的 ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError when using async pipe Angular 2 单元测试:自定义管道错误找不到管道 - Angular 2 Unit Test: Custom Pipe error The pipe could not be found 如何为角度 4 的自定义排序管道编写单元测试用例? - How to write unit test cases for custom sort pipe in angular 4? 在管道单元测试中进行模拟时服务未定义 - Service undefined when mocking inside Pipe unit test 当流/事件有管道时单元测试失败(takeUntil(destroy)) - Unit test fails when stream/event has pipe(takeUntil(destroy)) 从服务订阅可观察时,角度单元测试管道不是功能 - Angular unit test pipe is not a function when subscribe observable from a service 单元测试:从 subject() 访问 pipe 时抛出错误 - Unit Test: throwing an error when access .pipe from subject() Angular 4管道单元测试不适用于基本管道 - Angular 4 pipe unit test not working for basic pipe Angular 反应性 Forms AddControl 在使用 PatchValue 时导致 ExpressionChangedAfterItHasBeenCheckedError - Angular Reactive Forms AddControl Causes ExpressionChangedAfterItHasBeenCheckedError When Using PatchValue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM