我需要测试是否使用特定参数(在本例中为18)调用了第三方函数(map.setZoom)
createMap() {
const map = this.variable;
map.locate({setView: true, enableHighAccuracy: true});
map.on('locationfound', (e) => {
map.setZoom(18);
});
}
我的规格如下:
let component: Component;
let fixture: ComponentFixture<Component>;
beforeEach(async(() => {
TestBed.configureTestingModule({
// declarations, imports, etc..
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(Component);
component = fixture.componentInstance;
});
it('should verify map was created with zoom 18', () => {
spyOn(component.variable, 'setZoom').and.callThrough();
component.createMap();
$(component.variable).trigger('on');
expect(component.variable.setZoom).toHaveBeenCalledWith(18);
});
问题是我要测试的功能在通过jQuery触发的自定义事件中,并且我不知道是否正确触发了它
答案 0 :(得分:0)
按照@Taplar的建议,我需要使用'locationfound'
而非'on'
进行触发。另外,与前端人员交谈时,我意识到on()
方法实际上是来自传单而不是来自jquery。传单使用fire()
方法触发事件。因此,最终的规格是:
it('should verify map was created with zoom 18', () => {
spyOn(component.variable, 'setZoom').and.callThrough();
component.createMap();
component.variable.fire('locationfound');
expect(component.variable.setZoom).toHaveBeenCalledWith(18);
});