【问题标题】:Stack level too Deep - Rspec堆栈级别太深 - Rspec
【发布时间】:2013-10-05 19:42:11
【问题描述】:

我见过很多这样的问题,但没有一个解决方案适合我。我有一个这样的测试:

describe RolesController do
  describe "#delet" do 

    context "When the user is logged in" do 
      let(:user) {FactoryGirl.create(:user)}
      let(:admin) {FactoryGirl.create(:admin)}
      let(:adminRole) {FactoryGirl.create(:adminRole)}

      it "Should allow admins to delete roles" do 
        sign_in admin
        put :destroy, :id => adminRole.id
      end
    end
  end
end

简单,简单,简单。然而我得到了典型的错误:

  1) RolesController#delet When the user is logged in Should allow admins to delete roles
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /home/adam/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:23

我都喜欢......什么?我再次阅读了几十个关于此的问题,这似乎与工厂女孩有关,但我看不出这里的问题是什么。我有大量其他测试可以毫无问题地实例化这样的基于工厂女孩的对象。

【问题讨论】:

  • 你的堆栈跟踪是什么样的?无论如何,您需要分享您的工厂以获得比相关问题更具体的帮助。
  • 小心let - 它很懒惰。当您的测试运行sign_in admin 时,它将在运行测试之前实例化admin,但不会实例化useradminRole
  • 该错误表明您可能触发了无限循环。这可能与您的工厂之间的周期性依赖有关。你能发布工厂定义吗?
  • 能否将sign_in admin 调用的代码连同adminadminRole 的工厂代码一起发布?
  • 这不是您要求的,但您应该将 adminRole 重命名为 admin_role。 Rubyists 使用snake_case 作为变量名。

标签: ruby-on-rails rspec ruby-on-rails-4 factory-bot


【解决方案1】:

不要在下面的代码块中使用您通过let 定义的变量,因为它会触发无限递归。

【讨论】:

    【解决方案2】:

    我认为一些命名冲突会导致此堆栈级别太深错误。

    let(:adminRole) { FactoryGirl.create(:adminRole) }
    

    您如何将adminRole 工厂重命名为:admin_role

    factory :admin_role do
      ...
    end
    

    并将let 更改为关注variable convention

    let(:admin_role) { FactoryGirl.create(:admin_role) }
    

    希望对您有所帮助。

    【讨论】:

    • 投反对票,因为提及命名约定对解决问题没有帮助。
    猜你喜欢
    • 2012-03-04
    • 2016-09-10
    • 2015-04-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2012-07-24
    相关资源
    最近更新 更多