我正在尝试为位于build hint的此组件添加单元测试.... 但是我收到一个错误,因为该组件与类型有依赖关系:" IterableDiffers"
export class DualListComponent implements DoCheck, OnChanges { ....
constructor(private differs: IterableDiffers) {} ...
我的第一次尝试就像这样:
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);
});
});
&#13;
运行后我得到的错误;&#34; npm test&#34;是:
TypeError: Cannot read property 'diff' of undefined
错误信息本身并不太清楚,但原因是因为尝试使用对象&#34;不同&#34;什么应该在构造函数中加载。
任何想法如何在测试中添加它?
更新1 按照Tiep Phan的说明并插入&#34; IterableDiffers&#34;作为提供者......我遇到了问题:&#34;无法解决IterableDiffers的所有参数&#34; ... 我可以理解错误,但不知道如何解决它...错误主要是因为IterableDiffers类的构造函数接受类型&#34; IterableDifferFactory&#34;的数组。
constructor(factories: IterableDifferFactory[]);