简体   繁体   English

组件单元测试不只是在Angular 2 App中测试组件?

[英]Component Unit Tests Not Testing Just the Component in My Angular 2 App?

I am trying to set up some unit tests in my Angular 2/AngularCLI app. 我正在尝试在Angular 2 / AngularCLI应用程序中设置一些单元测试。 While the app is working as expected, the testing shows failures. 当应用程序按预期运行时,测试显示失败。

So, to start simply (or so I thought), I set up a test component which I generated from the command line (ng gc testr). 因此,以简单的方式开始(或我认为),我设置了一个从命令行(ng gc testr)生成的测试组件。 This creates a basic component test, along with the component files. 这将创建基本的组件测试以及组件文件。 When I CD into this component, and then run this built-in test with "ng test", I get errors related to other components in my app. 当我将CD插入该组件,然后使用“ ng test”运行此内置测试时,出现了与我的应用程序中其他组件相关的错误。 This is confusing to me because I thought the individual component tests were unit tests - therefore, only designed to test that specific component. 这让我感到困惑,因为我认为单个组件测试是单元测试-因此,仅用于测试该特定组件。 This is the example test for the component: 这是该组件的示例测试:

/* tslint:disable:no-unused-variable */
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { TestrComponent } from './testr.component';

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

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

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

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

I assume this should be working "out of the box" - because it's set up automatically by the AngularCLI. 我认为这应该“开箱即用”-因为它是由AngularCLI自动设置的。 So why the failure? 那为什么失败呢? Am I missing something here? 我在这里想念什么吗? Isn't the test code here designed to test the internal component, and not its relation to other components in the app? 这里的测试代码不是设计用来测试内部组件,也不是它与应用程序中其他组件的关系吗?

Here's an example of one of the errors I'm getting. 这是我遇到的错误之一的示例。 Clearly it's for a different component entirely. 显然,它完全适用于其他组件。 And I'm getting numerous errors like this for various components just from running this one test. 仅通过运行此测试,我就为各种组件收到了许多这样的错误。

ERROR in ./src/app/app.component.ts
Module not found: Error: Can't resolve 'app/app.component.html' in '/Users/mko/Documents/abc-cli/abc-cli/cli-abc/src/app'
 @ ./src/app/app.component.ts 77:22-55
 @ ./src/app/app.component.spec.ts
 @ ./src \.spec\.ts
 @ ./src/test.ts

Here's another error pertaining again to a different component: 这是另一个与另一个组件有关的错误:

ERROR in ./src/app/views/client/client-panel.component.ts
Module not found: Error: Can't resolve 'app/views/client/client-panel.component.html' in '/Users/mko/Documents/abc-cli/abc-cli/cli-abc/src/app/views/client'
 @ ./src/app/views/client/client-panel.component.ts 34:22-88
 @ ./src/app/views/client/client-panel.component.spec.ts
 @ ./src \.spec\.ts
 @ ./src/test.ts

If I cd into a component and run "ng test", does or does that not run just the unit test for that component? 如果我进入一个组件并运行“ ng test”,那么是否或者不仅仅对该组件进行单元测试? And, if not, what do I need to change to make this just run the individual component test, and not all of them? 而且,如果没有,我需要更改什么以使其仅运行单个组件测试,而不是全部运行?

So I figured out the problem. 所以我找出了问题所在。 Counter to what I assumed, out-of-the-box all individual component spec tests will run together because they are grouped together in a describe block. 与我的假设相反,开箱即用的所有单个组件规格测试将一起运行,因为它们被组合在一个describe块中。 So cd'ing into a certain component and running "ng test" will NOT run just that test. 因此,进入某个组件并运行“ ng test”将不会仅运行该测试。 It will in fact run ALL of them. 实际上它将运行所有这些。

You can, however, run an individual component test by prefixing the describe function with something, like with f: 但是,您可以通过在describe函数前面加上一些前缀(例如f)来运行单个组件测试:

fdescribe('SomeComponent', () => {...} fdescribe('SomeComponent',()=> {...}

If such a function exists, that will run, and no others. 如果存在这样的功能,那么它将运行,并且没有其他功能。

But, more importantly, the takeaway here is that "ng test" is actually designed to run an entire SERIES of unit tests, not a single unit test. 但是,更重要的是,这里的要点是,“ ng test”实际上是设计为运行整个系列的单元测试,而不是单个单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM