为组件生成的默认规范文件说明

default spec file explanation that gets generated for component

我使用 ng generate component test 创建了一个组件,但生成的文件很少。有一个文件名test.component.spec.ts。它有这个内容。

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

import { TestComponent } from './donations-pay.component';

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

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

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

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

我很难理解这一点。请帮帮我。 谢谢

当您开始 Angular 开发时,这可能会让您感到困惑。 :)

'spec' 文件用于对 Angular 服务、组件等进行单元测试,

这是 CLI 生成的测试文件,在您生成组件 TestComponent 时由 Angular CLI 为您设置。详细信息在官方 Angular 测试文档中,尤其是 here.

部分

Angular 测试文档绝对是学习测试的地方,但还有其他资源,例如 this

希望对您有所帮助。