angular - 有没有办法自动配置 component.spec.ts?

标签 angular angular-test

我正在我的 Angular 7 应用程序中添加单元测试。 我至少有 100 个组件要测试,并且每个组件都因配置而失败:它们需要声明所需的每个依赖项。

这是我的 component.spec.ts 我执行 ng test 时的配置在哪里:

    import { async, ComponentFixture, TestBed } from 
    '@angular/core/testing';

    import { myComponent } from './mycomponent';
    import { FontAwesomeModule } from '@fortawesome/angular- 
    fontawesome';

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

      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ myComponent ],
          imports: [
            FontAwesomeModule
            // + Others imports
          ]
        })
        .compileComponents();
      }));

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

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

在某些组件中,我添加了提供者。在某些情况下,我使用模拟服务。我所做的一切,都来自 angular docs .

有没有办法用 Angular 轻松或自动配置单元测试(或端到端测试),而不是手动添加每个需要的模块?

我正在使用 Angular 7、jasmine (3.3.1) 和 karma (4.0.0)。

最佳答案

我通常单独导入所有依赖项,因为我确保测试只加载它实际需要的依赖项。但是,我找到了一种方法,可以轻松地使所有依赖项都可用于您的测试脚本,而无需单独导入每个依赖项。不是单独导入所有依赖项,而是导入声明要测试的组件的模块。通过在单元测试中导入模块,您将使所有依赖项(包括服务和组件本身)可用于测试。

我通常不厌其烦地声明依赖关系,以避免用它不会使用的代码使测试过载,理论上,这通常会使运行测试运行得更慢。听起来这对您的用例来说可能没问题,因为您要编写很多测试。

除了速度上的损失外,我知道没有其他缺点,但可能会有。如果有人知道,请将它们作为评论添加到这篇文章中。

附言您的 AppModule 可能正在导入依赖项。这些可能需要与组件的声明模块一起单独导入。

测试脚本

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { EasyImportComponent } from './easy-import.component';
import { EasyImportModule } from './easy-import.module';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ EasyImportModule ]
        //, declarations: [ EasyImportComponent ] <-- No longer needed since module declares this already
    })
    .compileComponents();
  }));

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

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

关于angular - 有没有办法自动配置 component.spec.ts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54716593/

相关文章:

javascript - 如何在 Angular 4 中动态生成的元素上触发点击事件?

javascript导入问题与import vs require

使用 *ngIf 进行 Angular 测试 DOM 元素

javascript - Angular 2如何从嵌套的 promise 中返回对象数组

Angular CLI - 从优化中排除包

angular - 如何在 Angular 服务中运行 toast 消息的单元测试用例?

angular - 错误 : clientWidth is not declared configurable in http://localhost:9877node_modules/jasmine-core/lib/jasmine-core/jasmine. js(第 4410 行)

angular - 为什么我收到此错误 : NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService?

java - waitForAngularRequestsToFinish()——有什么用?

twitter-bootstrap - Angular 2 Bootstrap 4 和 ng-bootstrap