Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular ng2 dragula单元测试“;dragulaService.setOptions不是一个函数;_Angular_Unit Testing_Ng2 Dragula - Fatal编程技术网

Angular ng2 dragula单元测试“;dragulaService.setOptions不是一个函数;

Angular ng2 dragula单元测试“;dragulaService.setOptions不是一个函数;,angular,unit-testing,ng2-dragula,Angular,Unit Testing,Ng2 Dragula,我想在有ng2 dragula的组件上运行单元测试 然而,当我在组件上运行一个简单的单元测试时,我得到了错误 TypeError:无法读取未定义的属性“setOptions” component.html <div [dragula]="'section-bag'" [dragulaModel]='form.sections'> <div *ngFor="let section of form.sections"> <div class="dr

我想在有ng2 dragula的组件上运行单元测试

然而,当我在组件上运行一个简单的单元测试时,我得到了错误

TypeError:无法读取未定义的属性“setOptions”

component.html

<div [dragula]="'section-bag'"
     [dragulaModel]='form.sections'>
  <div *ngFor="let section of form.sections">
    <div class="drag-handle__section"></div>
  </div>
</div>
组件规格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');
      }
    });
  }

  ...
}
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();
  });

});
从“ng2 dragula”导入{DragulaService};
...
描述('MyComponent'的测试,()=>{
let comp:MyComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[MyComponent],
供应商:[
{provide:DragulaService}
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(MyComponent);
comp=夹具。组件状态;
comp.form.sections=[];
comp.form.sections.push(新FormSection());
fixture.detectChanges();
});
它('应该创建',()=>{
expect(comp.toBeTruthy();
});
});
我猜Dragula没有被正确导入,那么如何在测试中添加Dragula作为提供者,以便在组件的构造函数中使用它呢

我已经查看了哪些似乎相关,但我无法解决错误


注意:我对单元测试Dragula不太在意;我想测试组件中的其他功能,但此错误不允许运行任何进一步的测试。

更改您提供
DragularService
的方式。如果你像这样提供

{provide:DragulaService}

然后,您应该提到它的
useClass
useValue
属性

例如:
{provide:DragulaService,useClass:MockService}

如果您对mock不感兴趣,那么直接将其添加到
提供程序
数组中

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ MyComponent ],
      providers: [
         DragulaService  // see this line
      ]
    })
    .compileComponents();
}));

谢谢@Amit Chigadani,我的英雄!你好@Amit Chigadani,你能帮我回答这个问题吗:。。。很抱歉这样发布,但我需要帮助。请告诉我是否有更好的方法。你好@Amit Chigadani,你能帮我回答这个问题吗。。我试着解释一下,再做些改变。