由于我两次尝试将消息发布到JSpec Google Group显然都失败了,我将在此发布.
我遇到了JSpec的问题,显然是通过某种测试进入无限递归循环(下图).有任何想法吗?我的代码有问题还是JSpec?我正在通过Ruby Gem运行JSpec 2.11.2.
错误是'RangeError:超出最大调用堆栈大小.' (Safari)和'InternalError:太多的递归'(FF/Mac).我可以使用Firebug控制台将一个项目添加到房间,没有错误.
要重现该问题,请使用"jspec init test"创建模板jspec项目.然后编辑以下文件,如下所示:
yourlib.core.js
var Game = {};
Game.item = function () {
var result = {
name : 'Undefined',
room : null
}
return result;
};
Game.room = function () {
var result = {
items : [],
addItem : function (name) {
var item = Game.item();
item.name = name;
item.room = this;
this.items.push(item);
return item;
}
};
return result;
};
Run Code Online (Sandbox Code Playgroud)
spec.core.js
describe 'Room'
before_each
room = Game.room()
end
describe 'addItem()'
before_each
potion = room.addItem('Potion')
key = room.addItem('Key')
end
//this is fine
it 'should return two different items'
key.should_not.be potion
end
//InternalError: too much recursion
it 'should not give recursion error'
key.should.be potion
end
end
end
Run Code Online (Sandbox Code Playgroud)
免责声明:我以前也没有听说过 JSpec(尽管如果您正在寻找 JSpec,Jasmine是一个不错的选择。
我唯一能想到的是“be”函数是如何工作的。如果它沿着对象图向下移动以查找两个项目是否相等,那么它可能会遇到循环依赖问题:即您在每个项目中引用您的房间,而该项目又具有您的项目,而该项目又具有您的房间和等等等等。这最终成为一个无限循环,be 函数无法有效地返回淹没堆栈,从而引发您所看到的错误。
像这样的东西(虽然没有比较,也:还没有测试或运行这段代码,将其作为解释上一段的伪代码):
function be(obj) {
for (var key in obj) {
if (typeof(obj[key]) === "object") {
be(obj[key]); // If you have circular dependencies, the recursion never ends
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3085 次 |
| 最近记录: |