首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angular CLI生成的"spec.ts“文件是做什么用的?

Angular CLI生成的"spec.ts“文件是做什么用的?
EN

Stack Overflow用户
提问于 2016-05-29 03:01:33
回答 2查看 157.6K关注 0票数 251

我是Angular 2的新手(一般来说是Angular ...)我发现它非常吸引人。我正在使用Angular CLI来生成和服务项目。它似乎工作得很好-尽管对于我的小学习项目来说,它产生的- but比我所需要的要多。

我注意到它会为项目中的每个Angular元素(组件、服务、管道等)生成spec.ts。我到处寻找,但没有找到这些文件的用途的解释。

这些构建文件在使用tsc时通常是隐藏的吗?我想知道是因为我想更改我创建的一个名称不佳的Component的名称,然后发现在这些spec.ts文件中也引用了该名称。

代码语言:javascript
复制
import {
  beforeEach,
  beforeEachProviders,
  describe,
  expect,
  it,
  inject,
} from '@angular/core/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { Component } from '@angular/core';
import { By } from '@angular/platform-browser';
import { PovLevelComponent } from './pov-level.component';

describe('Component: PovLevel', () => {
  let builder: TestComponentBuilder;

  beforeEachProviders(() => [PovLevelComponent]);
  beforeEach(inject([TestComponentBuilder], function (tcb: TestComponentBuilder) {
    builder = tcb;
  }));

  it('should inject the component', inject([PovLevelComponent],
      (component: PovLevelComponent) => {
    expect(component).toBeTruthy();
  }));

  it('should create the component', inject([], () => {
    return builder.createAsync(PovLevelComponentTestController)
      .then((fixture: ComponentFixture<any>) => {
        let query = fixture.debugElement.query(By.directive(PovLevelComponent));
        expect(query).toBeTruthy();
        expect(query.componentInstance).toBeTruthy();
      });
  }));
});

@Component({
  selector: 'test',
  template: `
    <app-pov-level></app-pov-level>
  `,
  directives: [PovLevelComponent]
})
class PovLevelComponentTestController {
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-29 03:13:54

规范文件是源文件的单元测试。Angular应用程序的约定是每个.ts文件都有一个.spec.ts文件。当您使用ng test命令时,它们是通过Karma test runner (https://karma-runner.github.io/)使用Jasmine javascript测试框架运行的。

你可以用它来做进一步的阅读:

https://angular.io/guide/testing

票数 313
EN

Stack Overflow用户

发布于 2018-07-20 01:55:12

.spec.ts文件用于单个组件的单元测试。您可以通过ng test运行Karma任务运行器。要查看特定组件的单元测试用例的代码覆盖率,请运行ng test --code-coverage

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37502809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档