Angular 如何触发openLayers 3';单击';来自业力测试

Angular 如何触发openLayers 3';单击';来自业力测试,angular,karma-jasmine,openlayers-3,karma-coverage,Angular,Karma Jasmine,Openlayers 3,Karma Coverage,我使用angular2和openlayers3在页面上显示地图。 单击地图上的标记时,使用以下单击功能显示弹出窗口- this.map.on('singleclick', (e) => { this.showWellDetails(e.pixel); }); 这很好,但我没有办法从我的业力测试中触发。 因此,这段代码没有被覆盖 编辑-在此处也为测试用例添加了代码- @Component({ selector: 'test-c

我使用angular2和openlayers3在页面上显示地图。 单击地图上的标记时,使用以下单击功能显示弹出窗口-

        this.map.on('singleclick', (e) => {
            this.showWellDetails(e.pixel);
        });
这很好,但我没有办法从我的业力测试中触发。 因此,这段代码没有被覆盖

编辑-在此处也为测试用例添加了代码-

@Component({
    selector: 'test-component-wrapper',
    template: '<map [markers]="markers"  [isDocked]="isDocked" ></map>',
})
class TestComponentWrapper {
    public isDocked = true;
    public markers = [
        {
            "latitude": 101.106074,
            "longitude": 1.307283,
            "name": "211/27-A17",
        },
        {
            "latitude": 61.034344,
            "longitude": 1.703716,
            "name": "211/29-A17",
        },
    ];
}
describe('MapComponent events', () => {
    let component: MapComponent;
    let fixture: ComponentFixture<TestComponentWrapper>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                TestComponentWrapper,
                MapComponent,
            ],
            schemas: [CUSTOM_ELEMENTS_SCHEMA]
        })
            .compileComponents();

        fixture = TestBed.createComponent(TestComponentWrapper);
        component = fixture.debugElement.children[0].componentInstance;
        fixture.detectChanges();
    }));

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

    it('should test showDetails functionality', () => {
        component.isDocked = false;
        component.initMap();
        expect(fixture.debugElement.query(By.css('#mapId'))).not.toBe(null);
        let pixel = [
            233.10227584838867,
            191.25,
        ];

        // I want to trigger 'singleClick' on map from here...
        // component.map.singleClick();
        // Since this is not working, I have to make showWellDetails function public and call it directly.
        //at least it adds coverage for this function, but the original block of code mentioned above remains uncovered.


        component.showWellDetails(pixel);
        expect(fixture.debugElement.query(By.css('#featureDetails'))).toBe(null);
        expect(component.locationCoordinate).toBeUndefined();
    });
    //other tests
});
@组件({
选择器:“测试组件包装器”,
模板:“”,
})
类TestComponentWrapper{
public isDocked=true;
公共标记=[
{
“纬度”:101.106074,
“经度”:1.307283,
“名称”:“211/27-A17”,
},
{
“纬度”:61.034344,
“经度”:1.703716,
“名称”:“211/29-A17”,
},
];
}
描述('MapComponent事件',()=>{
let组件:MapComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[
TestComponentWrapper,
MapComponent,
],
模式:[自定义元素\u模式]
})
.compileComponents();
fixture=TestBed.createComponent(TestComponentWrapper);
component=fixture.debugElement.children[0]。componentInstance;
fixture.detectChanges();
}));
它('应该创建',()=>{
expect(component.toBeTruthy();
});
它('应该测试showDetails功能',()=>{
component.isDocked=假;
initMap();
expect(fixture.debugElement.query(By.css('#mapId')).not.toBe(null);
设像素=[
233.10227584838867,
191.25,
];
//我想从这里触发地图上的“单点点击”。。。
//component.map.singleClick();
//由于这不起作用,我必须公开showWellDetails函数并直接调用它。
//至少它增加了这个函数的覆盖范围,但是上面提到的原始代码块仍然没有被覆盖。
组件。showWellDetails(像素);
expect(fixture.debugElement.query(By.css('#featureDetails')).toBe(null);
expect(component.locationCoordinate).toBeUndefined();
});
//其他测试
});

感谢您的帮助!TIA。

我最终是如何实现这一目标的,这可能会帮助其他正在寻找此信息的人-

public singleClickCallback = (e) => {
        this.showWellDetails(e.pixel);
 }

 this.map.on('singleclick', this.singleClickCallback);

请问,你能发布你的单元测试代码吗?@SrAxi-在post edit中添加了测试代码嘿-有人能帮我吗?