我有一个使用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]使测试通过,但这似乎不是正确的做法。
发布于 2017-02-07 15:27:40
最后,测试隔离的问题是由在某些测试中定义的超出describe()范围的beforeEach()函数造成的。这导致套件中的所有测试都运行了这些beforeEach(),并且我遇到了上面描述的副作用。
https://stackoverflow.com/questions/42030685
复制相似问题