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
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):