With my two attempts at getting a message posted to the JSpec Google Group having apparently failed, I'm posting here instead.
I'm having trouble with JSpec apparently going into an infinite recursive loop with a certain kind of test (below). Any ideas? It there something wrong with my code or is it JSpec? I'm Running JSpec 2.11.2 via Ruby Gem.
The errors are 'RangeError: Maximum call stack size exceeded.' (Safari) and 'InternalError: too much recursion' (FF/Mac). I can add an Item to a Room using the Firebug console, with no errors.
To reproduce the problem, create a template jspec project using 'jspec init test'. Then edit the following files like so:
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;
};
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
The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is not being met and is, therefore, calling the function again and again until you hit the call stack limit.
How to Avoid RangeError: Maximum Call Stack Size Exceeded. If this error is encountered when calling recursive functions, it should be ensured that the function has a defined base case to terminate the recursive calls.
The JavaScript exception "too much recursion" or "Maximum call stack size exceeded" occurs when there are too many function calls, or a function is missing a base case.
Disclaimer: I also haven't heard of JSpec before (although Jasmine is a good alternative if you're looking for one.
The only thing that I can think of is how the 'be' function works. If its travelling down the object graph to find whether two items are equal, then it might run into the circular dependency hiccup: i.e. you're referencing your room in each item, which in turn has your items, which in turn has your rooms and so on and so forth. This ends up being an infinite loop from which the be function cannot return effectively flooding the stack and thus throwing the error you're seeing.
Something like this (without the comparison though, also: haven't tested or run this code, take it as pseudocode for explaining the above paragraph):
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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With