单元测试Angular:组件未定义且发生奇怪的错误“ _angular_core_testing__WEBPACK_IMPORTED_MODULE_2 __。ɵMetadataOverrider”

时间:2019-12-01 20:55:22

标签: angular unit-testing

我已经构建了一个没有任何规范文件的应用程序。

我使用angular-spec-generator包创建了所有规格文件。

当我运行ng test时,生成的app.component.spec.ts出现奇怪的错误:

  • 失败:
    _angular_core_testing__WEBPACK_IMPORTED_MODULE_2 __。ɵMetadataOverrider不是构造函数
  • TypeError:无法读取null的属性“ getComponentFromError”
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { AppComponent } from './app.component';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

describe('AppComponent', () => {
    let component: AppComponent;
    let fixture: ComponentFixture<AppComponent>;

  beforeEach(async(() => {
    // I tryed to add theses line but nothing change
    // **https://stackoverflow.com/a/49163912/11730828**
    TestBed.resetTestEnvironment();
    TestBed.initTestEnvironment(BrowserDynamicTestingModule,
      platformBrowserDynamicTesting());
        TestBed.configureTestingModule({
            declarations: [AppComponent]
        })
            .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(AppComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

这是app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>',
})
export class AppComponent { }

我正在与UT一起乞讨,这两个奇怪的错误无助于我调试,当我在google上找到任何东西

编辑:测试失败,组件未定义

致谢

0 个答案:

没有答案