JSpec - RangeError:超出最大调用堆栈大小

发布于 2024-08-13 08:31:43 字数 1367 浏览 0 评论 0原文

我两次尝试将消息发布到 JSpec Google Group 显然都失败了,我而是在这里发帖。

我遇到了问题 JSpec 显然进入了无限递归循环与某种测试(下面)。有什么想法吗?我的代码有问题还是 JSpec 有问题?我正在通过 Ruby Gem 运行 JSpec 2.11.2。

错误为“RangeError:超出最大调用堆栈大小”。 (Safari) 和“内部错误:递归过多”(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;
};

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-08-20 08:31:43

免责声明:我之前也没有听说过 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
    }
  }
}

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
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文