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 导入的库-模拟方法和排除_Angular_Jasmine - Fatal编程技术网

Angular 导入的库-模拟方法和排除

Angular 导入的库-模拟方法和排除,angular,jasmine,Angular,Jasmine,我有一个组件,其中我使用了一个方法,create(),来自外部库(Zoid)。我试图模拟这个方法,但我不知道如何在不让这个方法属于对象的情况下实现它。另外,我从Zoid库中得到一些JS错误,但是在我的单元测试中,我根本不想导入库。任何帮助都将不胜感激 我的组件的相关部分: import { create } from 'zoid/dist/zoid'; ngOnInit() { // Initialize the zoid instance this.widgetInstance =

我有一个组件,其中我使用了一个方法,
create()
,来自外部库(Zoid)。我试图模拟这个方法,但我不知道如何在不让这个方法属于对象的情况下实现它。另外,我从Zoid库中得到一些JS错误,但是在我的单元测试中,我根本不想导入库。任何帮助都将不胜感激

我的组件的相关部分:

import { create } from 'zoid/dist/zoid';

ngOnInit() {
  // Initialize the zoid instance
  this.widgetInstance = zoid.create({
    tag: 'payment-widget',
    url: this.iframeSrc,
    dimensions: {
      width: '100%',
      height: '600px',
    }
  });
}
这是我的单元测试

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PaymentWidgetComponent } from './payment-widget.component';

const zoid = {
  create: () => {
    return {};
  }
};   

describe('PaymentWidgetComponent', () => {
  let component: PaymentWidgetComponent;
  let fixture: ComponentFixture<PaymentWidgetComponent>;
  const mockWidgetInstance = jasmine.createSpyObj('WidgetInstance', ['render']);

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [PaymentWidgetComponent]
    })
      .compileComponents();
  }));   

  beforeEach(() => {
    fixture = TestBed.createComponent(PaymentWidgetComponent);
    component = fixture.componentInstance;
    component.widgetInstance = mockWidgetInstance;
    fixture.detectChanges();
  });   

  it('should create', () => {
    expect(component).toBeTruthy();
  });

});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“./payment widget.component”导入{PaymentWidgetComponent};
const zoid={
创建:()=>{
返回{};
}
};   
描述('PaymentWidgetComponent',()=>{
let组件:PaymentWidgetComponent;
let夹具:组件夹具;
const mockWidgetInstance=jasmine.createSpyObj('WidgetInstance',['render']);
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[PaymentWidgetComponent]
})
.compileComponents();
}));   
在每个之前(()=>{
fixture=TestBed.createComponent(PaymentWidgetComponent);
组件=fixture.componentInstance;
component.widgetInstance=mockWidgetInstance;
fixture.detectChanges();
});   
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});

我认为您还必须将其导入到单元测试文件中

试试这个:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PaymentWidgetComponent } from './payment-widget.component';
import * as zoid from 'zoid/dist/zoid'; // import everything like so
 

describe('PaymentWidgetComponent', () => {
  let component: PaymentWidgetComponent;
  let fixture: ComponentFixture<PaymentWidgetComponent>;
  const mockWidgetInstance = jasmine.createSpyObj('WidgetInstance', ['render']);

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [PaymentWidgetComponent]
    })
      .compileComponents();
  }));   

  beforeEach(() => {
    fixture = TestBed.createComponent(PaymentWidgetComponent);
    component = fixture.componentInstance;
    spyOn(zoid, 'create'); // spy on zoid.create and make it return undefined
    component.widgetInstance = mockWidgetInstance; // you're mocking widgetInstance here so it should be fine.
    fixture.detectChanges();
  });   

  it('should create', () => {
    expect(component).toBeTruthy();
  });

});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“./payment widget.component”导入{PaymentWidgetComponent};
从“zoid/dist/zoid”导入*作为zoid;//像这样进口任何东西
描述('PaymentWidgetComponent',()=>{
let组件:PaymentWidgetComponent;
let夹具:组件夹具;
const mockWidgetInstance=jasmine.createSpyObj('WidgetInstance',['render']);
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[PaymentWidgetComponent]
})
.compileComponents();
}));   
在每个之前(()=>{
fixture=TestBed.createComponent(PaymentWidgetComponent);
组件=fixture.componentInstance;
spyOn(zoid,'create');//监视zoid.create并使其返回未定义
component.widgetInstance=mockWidgetInstance;//您在这里模拟的是widgetInstance,所以应该可以。
fixture.detectChanges();
});   
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});