如何从业力测试

时间:2017-06-01 14:35:58

标签: angular karma-jasmine openlayers-3 karma-coverage

我正在使用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
});

任何帮助表示赞赏! TIA。

1 个答案:

答案 0 :(得分:0)

我最终如何实现它可能会帮助那些正在寻找这些信息的人 -

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

 this.map.on('singleclick', this.singleClickCallback);
相关问题