gpt4 book ai didi

javascript - undefined 不是带有 Array.prototype.find 的 PhantomJS 上的 Mocha/Chai/Sinon 中的构造函数

转载 作者:行者123 更新时间:2023-11-30 07:55:07 24 4
gpt4 key购买 nike

我现在对此非常头疼。我在尝试测试的方法中有一行代码,无论我做什么我都无法通过。有用!在“现实世界”中没有任何问题。

我发现每次我深入研究 JS 单元测试时都会遇到这样的障碍,这让我怀疑它是否真的值得。

这是我要测试的方法...

    _mapStopsAroundHub(managerContext, soCode, homePostcode, homeStops, changeLinkedStopCallback) {
homeStops = homeStops || [];
MappingDataGateway.getNearbyStops(soCode, homePostcode, Cfg.DefaultNearbyStopDistance, (stops, status, xhr) => {
if(status == MappingDataGateway.Status.OK) {
stops.forEach((element) => {
let isHomeStop = homeStops.length > 0 && (homeStops.find(hs => hs.id === element.id) !== undefined);
let markerColor = isHomeStop ? "green" : "orange";
let marker = managerContext.addBusStopMarker(element, markerColor);
if (changeLinkedStopCallback) {
managerContext._api.event.addListener(marker, 'click', () => {
let newIcon = marker.icon.includes("orange") ? `/images/fa-bus-green.png` : `/images/fa-bus-orange.png`;
marker.setIcon(newIcon);
changeLinkedStopCallback(marker.data);
})
}
});
}
else if (errorCallback) { errorCallback(xhr); }
});
}

我添加了“isHomeStop”变量和完全不必要的 homeSTops.length 检查,让一些测试用例通过,但我无法测试 homeStops 数组包含数据的情况,因为 Mocha 一直如此有用的错误消息“undefined is not a constructor”类型的消息。完整错误如下...

PhantomJS 2.1.1 (Windows 8 0.0.0) MapsManager can interract with a loaded map to map the stops around a hub point directly with a linked stop mapping all selectable stops in the area showing selected linked stops in a different colour FAILED undefined is not a constructor (evaluating 'homeStops.find(function (hs) { return hs.id === element.id; })')

失败的测试看起来像这样......

  it("directly with a linked stop mapping all selectable stops in the area showing selected linked stops in a different colour", () => {
let allStops = [{ id: 1 }, { id: 2 }, { id: 3 }];
let homeStops = [{ id: 2 }];
let mappingDataStub = Sinon.stub(MappingDataGateway, 'getNearbyStops');
mappingDataStub.yields(allStops, "OK");
let addMarkerStub = Sinon.stub(objUt, 'addBusStopMarker');

objUt._mapStopsAroundHub(objUt, testSoCode, testPostCode, homeStops);
mappingDataStub.restore();
addMarkerStub.restore();

Sinon.assert.calledThrice(addMarkerStub);
Sinon.assert.calledWith(addMarkerStub, allStops[0], "orange");
Sinon.assert.calledWith(addMarkerStub, allStops[1], "green");
Sinon.assert.calledWith(addMarkerStub, allStops[2], "orange");
});

我在代码中放置了未定义的检查来检查对象的状态,它就在那里——它似乎落在了 find 中的 lambda 表达式上。

附加信息:这似乎只发生在 PhantomJS 上——如果我使用 Chrome,一切都运行良好(尽管这不是一个可行的 CI 选项!)。

最佳答案

这似乎是 PhantomJS 的问题。

我通过从这里应用 PhantomJS polyfil 来修复它...

https://www.npmjs.com/package/phantomjs-polyfill-find

(糟透了......)

关于javascript - undefined 不是带有 Array.prototype.find 的 PhantomJS 上的 Mocha/Chai/Sinon 中的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com