Angular8单元测试-显示错误:“错误:非法状态:无法加载指令ComponentName的摘要”

时间:2019-10-16 09:38:59

标签: angular unit-testing jasmine karma-runner

在我的项目中,我仅启用了PathNotFoundComponent的spec文件,所有其他spec.ts文件都被注释了,并且出现了以上错误。

我的组件文件,

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-path-not-found',
  templateUrl: './path-not-found.component.html',
  styleUrls: ['./path-not-found.component.scss']
})
export class PathNotFoundComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

我的组件规格文件

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PathNotFoundComponent } from './path-not-found.component';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ PathNotFoundComponent ]
    })
    .compileComponents();
  }));

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

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

但是此测试用例在以下情况下有效。

  

如果我将组件规范文件中的beforeEach更改为beforeAll。

  

在test.ts文件中将require.context('./',true,/.spec.ts$/)更改为   require.context('./',true,/path-not-found.component.spec.ts $ /)

请帮助我找到解决方案?

0 个答案:

没有答案
相关问题