ng2-dragula 单元测试 "dragulaService.setOptions is not a function"
ng2-dragula unit test "dragulaService.setOptions is not a function"
我想 运行 对我的具有 ng2-dragula 的组件进行单元测试。
但是,当我 运行 对组件进行简单的单元测试时,出现错误
TypeError: Cannot read property 'setOptions' of undefined
component.html
<div [dragula]="'section-bag'"
[dragulaModel]='form.sections'>
<div *ngFor="let section of form.sections">
<div class="drag-handle__section"></div>
</div>
</div>
component.ts
import { Component, Input } from '@angular/core';
import { DragulaService } from 'ng2-dragula';
...
export class MyComponent {
@Input() form;
constructor(private dragulaService: DragulaService) {
dragulaService.setOptions('section-bag', {
moves: (el, container, handle) => {
return handle.classList.contains('drag-handle__section');
}
});
}
...
}
component.spec.ts
import { DragulaService } from 'ng2-dragula';
...
describe('Test for MyComponent', () => {
let comp: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ],
providers: [
{ provide: DragulaService }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
comp = fixture.componentInstance;
comp.form.sections = [];
comp.form.sections.push(new FormSection());
fixture.detectChanges();
});
it('should create', () => {
expect(comp).toBeTruthy();
});
});
我猜 Dragula 没有被正确导入,那么如何在测试中将 Dragula 添加为提供者以在组件的构造函数中使用它?
我查看了 ,其中 似乎 相关,但我无法解决错误。
注意:我对 Dragula 的单元测试不是很在意;我想测试组件中的其他功能,但此错误不允许对 运行.
进行任何进一步测试
更改您提供 DragulaService
的方式。如果你提供它
{ provide: DragulaService }
那么您应该提及 useClass
或 useValue
属性。
例如:{ provide: DragulaService, useClass : MockService }
如果你没有兴趣模拟,那么直接将它添加到providers
数组中。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ],
providers: [
DragulaService // see this line
]
})
.compileComponents();
}));
我想 运行 对我的具有 ng2-dragula 的组件进行单元测试。
但是,当我 运行 对组件进行简单的单元测试时,出现错误
TypeError: Cannot read property 'setOptions' of undefined
component.html
<div [dragula]="'section-bag'"
[dragulaModel]='form.sections'>
<div *ngFor="let section of form.sections">
<div class="drag-handle__section"></div>
</div>
</div>
component.ts
import { Component, Input } from '@angular/core';
import { DragulaService } from 'ng2-dragula';
...
export class MyComponent {
@Input() form;
constructor(private dragulaService: DragulaService) {
dragulaService.setOptions('section-bag', {
moves: (el, container, handle) => {
return handle.classList.contains('drag-handle__section');
}
});
}
...
}
component.spec.ts
import { DragulaService } from 'ng2-dragula';
...
describe('Test for MyComponent', () => {
let comp: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ],
providers: [
{ provide: DragulaService }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
comp = fixture.componentInstance;
comp.form.sections = [];
comp.form.sections.push(new FormSection());
fixture.detectChanges();
});
it('should create', () => {
expect(comp).toBeTruthy();
});
});
我猜 Dragula 没有被正确导入,那么如何在测试中将 Dragula 添加为提供者以在组件的构造函数中使用它?
我查看了
注意:我对 Dragula 的单元测试不是很在意;我想测试组件中的其他功能,但此错误不允许对 运行.
进行任何进一步测试更改您提供 DragulaService
的方式。如果你提供它
{ provide: DragulaService }
那么您应该提及 useClass
或 useValue
属性。
例如:{ provide: DragulaService, useClass : MockService }
如果你没有兴趣模拟,那么直接将它添加到providers
数组中。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ],
providers: [
DragulaService // see this line
]
})
.compileComponents();
}));