【问题标题】:Angular test gives error - Failed: Cannot read property 'className' of undefined角度测试给出错误 - 失败:无法读取未定义的属性“className”
【发布时间】:2021-10-08 23:46:53
【问题描述】:

以下是我的规范。它运行良好,所有测试用例都通过了。不知何故 3 次 2 次测试失败并出现错误 Failed: Cannot read property 'className' of undefined。我从未在文件中的任何地方使用过 className。但是,我正在使用 Syncfusion 库进行对话。我不明白出了什么问题。偶尔,它也会给我这个错误。 Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)。我尝试使用doneFn 也增加间隔。它仍然给出这个错误。

*sync.spec.ts

describe("SettingDialogComponent", () => {
  let component: SettingDialogComponent;
  let fixture: ComponentFixture < SettingDialogComponent > ;
  const elementId = "1234567890";
  const parentId = "0987654321";
  const relearnCommand = "Re-learning";
  let service: Service;
  const element = new Element(elementId, [new Data("limit", "2.00")], "", parentId, [], [], "");
  let heroDe: DebugElement;
  let heroEl: HTMLElement;

  beforeEach(
    waitForAsync(async() => {
      await TestBed.configureTestingModule({
          declarations: [DialogComponent, SettingDialogComponent],
          imports: [
            DialogModule,
            FormsModule,
            HttpClientModule,
            ReactiveFormsModule,
            TranslateModule,
            TranslateModule.forRoot({
              loader: {
                provide: TranslateLoader,
                useClass: TestTranslateLoader,
              },
            }),
          ],
          providers: [{
              provide: Service,
              useValue: {
                getElement: (): Observable < Element > => {
                  return of(element);
                },
                some: () => of (true),
                update: () => of (true),
                runCommand: () => of (true),
              },
            },
            HttpClient,
            TranslateService,
          ],
        })
        .compileComponents()
        .then(() => {
          fixture = TestBed.createComponent(SettingDialogComponent);
          component = fixture.componentInstance;
          component.elementId = elementId;
          service = fixture.debugElement.injector.get(AssetService);
          spyOn(service, "update").and.callThrough();
          spyOn(service, "getElement").withArgs(component.elementId).and.returnValue( of (element));
          spyOn(service, "runCommand").and.callThrough();
        });
    }),
  );

  it(
    "when show open modal",
    waitForAsync(async() => {
      fixture.detectChanges();
      await fixture.whenStable();

      component.show(); //opens syncfusion dialog
      fixture.detectChanges();

      const shownDialog = fixture.debugElement.queryAll(By.css(".e-popup-open"));
      expect(shownDialog).not.toBeNull();

      component.hide(); //hides syncfusion dialog
    }),
  );

});

无论我在哪里使用fixture.debugElement.query(By.css(""));,都会显示此错误。它说 debugElement 的值为 null。

*我的 sync.ts 文件

@ViewChild("editor") public deviceEditor: DialogComponent;

public show(): void {
    this.thresholdSettings.reset();
    this.Service.getElement(this.elementId).subscribe((result) => {
      this.element = result;
      this.limit = getMetadataValue(this.element.data, "limit");
    });
    this.deviceEditor.show();
  }
<ejs-dialog #deviceEditor
            id="dialog"
            isModal="true"
            class="device-threshold-dialog">
</ejs-dialog>

错误堆栈跟踪:

TypeError:无法读取未定义的属性“className” 在 removeClass (http://localhost:9877/karma_webpack/webpack:/node_modules/@syncfusion/ej2-base/dist/es6/ej2-base.es2015.js:5019:1) 在 DialogComponent.destroy (http://localhost:9877/karma_webpack/webpack:/node_modules/@syncfusion/ej2-popups/dist/es6/ej2-popups.es2015.js:3072:20) 在 http://localhost:9877/karma_webpack/webpack:/node_modules/@syncfusion/ej2-angular-base/src/component-base.js:149:1 在计时器(http://localhost:9877/karma_webpack/webpack:/node_modules/zone.js/fesm2015/zone.js:2561:1) 在 ZoneDelegate.invokeTask (http://localhost:9877/karma_webpack/webpack:/node_modules/zone.js/fesm2015/zone.js:406:1) 在 AsyncTestZoneSpec.onInvokeTask (http://localhost:9877/karma_webpack/webpack:/node_modules/zone.js/fesm2015/zone-testing.js:1180:1) 在 ProxyZoneSpec.onInvokeTask (http://localhost:9877/karma_webpack/webpack:/node_modules/zone.js/fesm2015/zone-testing.js:315:1) 在 ZoneDelegate.invokeTask (http://localhost:9877/karma_webpack/webpack:/node_modules/zone.js/fesm2015/zone.js:405:1) 在 Zone.runTask (http://localhost:9877/karma_webpack/webpack:/node_modules/zone.js/fesm2015/zone.js:178:1) 在 invokeTask (http://localhost:9877/karma_webpack/webpack:/node_modules/zone.js/fesm2015/zone.js:487:1)

【问题讨论】:

    标签: angular jasmine karma-jasmine syncfusion angular-test


    【解决方案1】:

    我们已经验证了您报告的查询,但我们无法使用您的共享代码 sn-ps 制作可运行的示例。

    我们注意到您在对话框组件中使用了错误的属性。我们建议您将对话框渲染部分中的类更改为 cssClass 并在问题解决后尝试一下

    代码sn-ps:

      <ejs-dialog #deviceEditor 
            id="dialog" 
            isModal="true" 
            cssClass="device-threshold-dialog"> 
      </ejs-dialog> 
    

    示例:https://stackblitz.com/edit/angular-s1dxxw?file=app.component.html

    【讨论】:

    • 好吧,这不是我的问题的原因。但是,是的,css 应该按照建议的方式进行。
    【解决方案2】:

    我在包括component.toBeTruthy(); 在内的每个测试用例中都使用await fixture.whenRenderingDone() 而不是await fixture.whenStable() 来解决这个问题。希望这可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 2019-08-22
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多