2016-06-29 33 views

回答

4

我真的很苦惱同一個問題。這裏是一個鏈接到一些測試與js測試庫'摩卡': http://blog.mathieu-leplatre.info/test-your-leaflet-applications-with-mocha.html

然而,我遇到了進一步的問題,試圖調用傳單的類型捕獲所有'L'功能。第一次是這樣的:

}(window, document)); 
^

ReferenceError: window is not defined 

我糾正這個問題有這段代碼:

// Create globals so leaflet can load 
GLOBAL.window = {}; 
GLOBAL.document = { 
    documentElement: { 
    style: {} 
    }, 
    getElementsByTagName: function() { return []; }, 
    createElement: function() { return {}; } 
}; 
GLOBAL.navigator = { 
    userAgent: 'nodejs' 
}; 
GLOBAL.L = require('leaflet'); 

Node.js Leaflet error

後,我處理了這個問題,我遇到了一個問題,與實際函數,如'L.map('')。看來這個函數需要一個帶有id的元素才能正常工作。

這是我該函數收到錯誤:

 return (typeof id === 'string' ? document.getElementById(id) : id); 
               ^

TypeError: document.getElementById is not a function 

我希望這可以幫助你一點點,我肯定還沒有計算出來。

1

我一直在努力測試和使用Leaflet。

目前我正在使用QUnit來運行測試。

我目前必須在瀏覽器中打開它,看看它是否工作,也許別人知道如何通過命令行運行QUnit。

在我編寫並運行我的測試之前,我看了一下Leafletjs文檔頁面,並開始使用google chrome上的開發人員工具控制檯探索不同的js對象。

單張文檔:從我的QUnit tests.js文件 http://leafletjs.com/reference-1.0.0.html

示例測試:

QUnit.test("map default options", function(assert) assert.equal(myMap.getCenter().toString(), 
      "LatLng(0, 8.846)", 
      "The map is centered at the ZMT's longitude, and the equator" 
    ); 
    assert.equal(myMap.getZoom(), 
      2, 
      "The default zoom is set to 2" 
    ); 
}); 

QUnit.test("baseLayer layerGroup", function(assert) { 
    assert.equal(baseLayer.getLayers().length, 
      1, 
      "There is just one layer in 'baseLayer' layerGroup" 
    ); 
    assert.equal(baseLayer.getLayers()[0]._url, 
      "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", 
      "The url of the layer leads to the correct openstreet map tiles" 
    ); 

    assert.equal(baseLayer.getLayers()[0].options.attribution, 
      '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>', 
      "The attribution for the layer is correct" 
    ); 
    assert.equal(baseLayer.getLayers()[0].options.minZoom, 
      0, 
      "The default minimum zoom is set to 0" 
    ); 
    assert.equal(baseLayer.getLayers()[0].options.maxZoom, 
      19, 
      "The default maximum zoom is set to 19" 
    ); 
});