如何在茉莉花测试框架中处理谷歌地图事件

Mr.*_*r.B 6 javascript javascript-events backbone.js jasmine

我正在尝试使用jasmine framwork为谷歌地图编写javascript测试.我想要做的是启动地图并更改边界(缩小)并测试地图是否正确缩小.

我遇到的问题是茉莉似乎没有办法处理事件.Jasmine有spyOn()方法,用于查找方法(不是事件)的用法.在jasmine中还有waits()方法等待特定的时间.这些方法都不适用于处理事件.有没有人有茉莉花活动的经验?

我正在使用的代码:

describe('Map view', function () {
    beforeEach(function () {
        $('body').append("<div data-role='page' id='page-map'><div id='map_canvas'></div></div>");

        this.view = new MapView({el: $('#map_canvas')});
    });

    afterEach(function () {
        $('body div#page-map').remove();
    });

    describe('zoom to a new bound in the map', function () {
        it('should set map bounds correctly', function () {
            this.view.zoomToBounds(this.fixtures.Locations.valid.bounds);

            google.maps.event.addListener(this.view.map, 'bounds_changed', function() {
                // expect() doesn't work in this context.. (ex: expect(5).toEqual(1) will pass)
                expect(this.getBounds().getSouthWest().lat()).toBeGreaterThan(self.fixtures.Locations.valid.bounds.minLat);
                expect(this.getBounds().getSouthWest().lng()).toBeGreaterThan(self.fixtures.Locations.valid.bounds.minLng);
                expect(this.getBounds().getNorthEast().lat()).toBeLessThan(self.fixtures.Locations.valid.bounds.maxLat);
                expect(this.getBounds().getNorthEast().lng()).toBeLessThan(self.fixtures.Locations.valid.bounds.maxLng);
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

骨干视图将启动,谷歌地图将呈现.zoomToBounds方法工作正常,但是当我想检查我遇到一些问题的结果时.在google.maps.event.addListener()子句中,(jasmine)expect()调用似乎不起作用.

最好的方法当然是使用jasmine方法直接捕获事件,但我还没有想出办法来做到这一点.

这里的任何人都知道如何处理这个问题?

Gra*_*ett 1

最好的方法当然是使用 jasmine 方法直接捕获事件

您是否尝试过使用茉莉花间谍来监视绑定了事件的对象的原型?我认为你的事件可能在间谍设置之前被绑定:

这是一个简单的例子(在咖啡脚本中)

  it 'calls render when a model is added to the collection', ->
    spyOn(MyView.prototype, 'render')
    view = new MyView({
      collection : new Backbone.Collection([])
    })
    view.collection.add({})
    expect(view.render).toHaveBeenCalled()
Run Code Online (Sandbox Code Playgroud)