2017-02-03 71 views
0

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

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]进行测试,但这似乎不对。

+0

你能给我们从控制台的全部输出吗? –

+0

@MezoIstvan请在这里找到 - https://drive.google.com/file/d/0B5ymVhu1SZm0d3pPQUhOT3RhaEk/view?usp=sharing(这里粘贴太大了)。 – sax

回答

0

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