Angular 2测试隔离(模板错误)

时间:2017-02-03 18:24:19

标签: angular testing angular-cli

我有一个使用angular-cli(1.0.0-beta.26)创建的Angular(2.4.5)应用程序,我遇到了测试困难。我有一个简单的组件:

import { Component } from '@angular/core';

@Component({
  selector: 'app-empty',
  templateUrl: './empty.component.html',
  styleUrls: ['./empty.component.css']
})
export class EmptyComponent { }

(.html和.css都是空文件)和单元测试:

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

import { EmptyComponent } from './empty.component';

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

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

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

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

启动npm run test时,测试失败并显示错误:

Uncaught Error: Template parse errors: Can't bind to 'value' since it
isn't a known property of 'app-detail-section-item'.

这与其他正在测试的组件有关。我不明白为什么这个其他组件会影响EmptyComponent的测试?是因为Webpack将测试捆绑在一起吗?我希望测试是孤立的。我可以使用schemas: [NO_ERRORS_SCHEMA]进行测试,但这似乎不对。

1 个答案:

答案 0 :(得分:0)

最后,测试隔离的问题是由某些测试中beforeEach()范围定义的describe()函数引起的。这导致了beforeEach()运行套件中的所有测试,我遇到了上述副作用。