繁体   English   中英

Angular 4 + Karma测试组件

[英]Angular 4 + Karma testing a component

我正在尝试测试将其父组件注入的组件:

 constructor( @Host() app: ParentComponent) { ... } 

运行测试时,我收到以下消息:

错误:没有ParentComponent的提供者

我已经包含了父组件和子组件中使用的所有依赖项。 我需要做一些特定的事情来注入主机组件吗?

这是我的完整测试:

 describe('ChildComponent: Component', () => { //declare the component let comp: ChildComponent; let ca: ComponentFixture<ChildComponent>; //setup //first the async call to compile all the external templates beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ ... ], declarations: [ ParentComponent, ChildComponent ], providers: [ ... ] }) .compileComponents(); })); //then the sync call to create the instance beforeEach(() => { ca = TestBed.createComponent(ChildComponent); comp = ca.componentInstance; ca.detectChanges(); }); afterEach(() => { comp = null; ca = null; }); //define the tests it('should have items...', () => { let count: number = comp.someArray.length; expect(count).toBeGreaterThan(0); }); }); 

您必须为此组件指定一个提供程序。

 TestBed.configureTestingModule({ imports: [ ... ], declarations: [ ParentComponent, ChildComponent ], providers: [ {provide: ParentComponent, useClass: ParentComponent} ] }) .compileComponents(); 

暂无
暂无

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

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