Rspec从关联中获得“堆栈级别太深”

时间:2011-11-04 15:15:07

标签: ruby-on-rails rspec

对于控制器测试,在规范的顶部我正在定义模型:

  let(:user) { Factory(:user, :company => company) }
  let(:facility) { Factory(:facility, :company => company) }
  let(:company) { Factory(:company, :facilities => [facility]) }

做基本的索引规范:

  context "responding to GET index" do
    it "should expose all facilities as @facilities" do
      user.company.should_receive(:facilities).and_return([facility])
      get :index
      assigns[:facilities].should == [facility]
    end
  end

但是我收到了这个错误:

should expose all facilities as @facilities
stack level too deep
/Users/dmix/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.6.4/lib/rspec/core/let.rb:90
88      module InstanceMethods
89        def __memoized # :nodoc:
90          @__memoized ||= {}
91        end
92      end

这个问题似乎同时引用了3个工厂。

如果我从公司工厂定义中删除:facility,则会出现堆栈错误:

let(:company) { Factory(:company) }

如何在不获取堆栈溢出的情况下生成三个相互引用的模型?

1 个答案:

答案 0 :(得分:2)

问题是您的facilitycompany个对象相互引用。我认为:facilities => [facility]工厂的company部分可能不必要,因为您已将facility对象的公司指定为company。试试这个:

let(:facility) { Factory(:facility, :company => company) }
let(:company) { Factory(:company) }