Avatar ·

Angular CLI 创建的 *.spec.ts 文件有什么用?

大家好!我刚开始学习 Angular 和 TypeScript。当我通过 Angular CLI 创建新组件或服务时(例如 `ng generate component my-component`),除了主要文件外,还会出现扩展名为 `.spec.ts` 的文件。例如 `my-component.component.spec.ts`。我不太明白它们的用途。 我的项目没有它们也能正常构建和运行。它们只会让文件浏览器变得杂乱。也许这是 CLI 的 bug,或者可以禁用它们?文档中提到过测试相关的内容,但我目前还没有写过任何测试。 以下是为一个简单组件生成的此类文件的示例: ```typescript import { ComponentFixture, TestBed } from '@angular/core/testing'; import { MyComponent } from './my-component.component'; describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ MyComponent ] }) ```