简体   繁体   English

如何将IterableDiffers“服务”注入到单元测试中

[英]how to inject IterableDiffers “service” into a unit test

I am trying add unit tests for this component located in https://github.com/czeckd/angular-dual-listbox .... But I got an error because the component has a dependency with the type: "IterableDiffers" 我正在尝试为位于https://github.com/czeckd/angular-dual-listbox的该组件添加单元测试...。但是我收到一个错误,因为该组件具有类型为“ IterableDiffers”的依赖项

export class DualListComponent implements DoCheck, OnChanges { ....
constructor(private differs: IterableDiffers) {} ...

My first try looks like on this way: 我的第一次尝试是这样的:

 import { async, ComponentFixture, TestBed} from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { DebugElement } from '@angular/core'; import { TranslateModule } from 'ng2-translate/ng2-translate'; import { DualListComponent } from './dual-list.component'; describe('DashboardHeroComponent when tested directly', () => { let comp: DualListComponent; let fixture: ComponentFixture<DualListComponent>; let heroEl: DebugElement; // async beforeEach beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [DualListComponent], imports: [ TranslateModule.forRoot()] }) .compileComponents(); // compile template and css })); // synchronous beforeEach beforeEach(() => { let srouce: Array < any > = [ { key: 1, station: 'Antonito', state: 'CO' }, { key: 2, station: 'Big Horn', state: 'NM' }, { key: 3, station: 'Sublette', state: 'NM' }, { key: 32, station: 'Eureka', state: 'CO' } ]; fixture = TestBed.createComponent(DualListComponent); comp = fixture.componentInstance; heroEl = fixture.debugElement.query(By.css('.hero')); // find hero element comp.key = 'key'; comp.display = 'station'; comp.source = srouce; comp.destination = []; fixture.detectChanges(); // trigger initial data binding }); it('should display hero name', () => { expect(comp.available.list.length).toEqual(4); }); }); 

And the error what I got after run ;"npm test" is: 而我在运行“ npm test”后得到的错误是:

TypeError: Cannot read property 'diff' of undefined   

the error message is not too clear itself but the reason is because try use the object "differs" what should be load in the constructor. 错误消息本身并不太清楚,原因是因为尝试使用对象“ differs”应在构造函数中加载的内容。

any idea how to add it in the test? 任何想法如何在测试中添加它?

update 1 after follow the instructions from Tiep Phan and insert "IterableDiffers" as a provider ... I got the issue: "Can't resolve all parameters for IterableDiffers"... I can understand the error but don't know how to solve it...the error basically is because the constructor of IterableDiffers class accept an array of type "IterableDifferFactory". 按照Tiep Phan的说明进行更新1 ,然后将“ IterableDiffers”作为提供程序插入...我遇到了问题:“无法解析IterableDiffers的所有参数” ...我可以理解该错误,但不知道如何解决它...错误基本上是因为IterableDiffers类的构造函数接受类型为“ IterableDifferFactory”的数组。

constructor(factories: IterableDifferFactory[]);

add your dependencies to NgModule declaration 将您的依赖项添加到NgModule声明中

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

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [DualListComponent],
        imports: [ TranslateModule.forRoot()],
        providers: [
            //other provider
            IterableDiffers
        ]
    })
        .compileComponents(); // compile template and css
}));

I don't understant why you want to inject IterableDiffers . 我不明白您为什么要注入IterableDiffers It works well. 它运作良好。

Your error happens here 您的错误发生在这里

buildAvailable(source:Array<any>) : boolean {
    let sourceChanges = this.sourceDiffer.diff(source);

this.sourceDiffer is undefined. this.sourceDiffer是未定义的。 If you take a look at source code you can notice that sourceDiffer is initialized within ngOnChanges . 如果看一下源代码,您会注意到sourceDifferngOnChanges被初始化。 So you need to call ngOnChanges 所以你需要打电话给ngOnChanges

Look at this article https://medium.com/@christophkrautz/testing-ngonchanges-in-angular-components-bbb3b4650ee8 看看这篇文章https://medium.com/@christophkrautz/testing-ngonchanges-in-angular-components-bbb3b4650ee8

Here is example for your case: 这是您的情况的示例:

fixture = TestBed.createComponent(DualListComponent);
comp = fixture.componentInstance;
heroEl = fixture.debugElement.query(By.css('.hero')); // find hero element
comp.key = 'key';
comp.display = 'station';
comp.source = srouce;
comp.destination = [];

comp.ngOnChanges({ // add this
    source: new SimpleChange(null, srouce, true)
});

See it in action in Plunker Example Plunker示例中查看实际操作

Another way is using temporary component as described in article above. 另一种方法是使用上面文章中所述的临时组件。

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

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