简体   繁体   English

RSPEC和工厂女孩SystemStackError:堆栈级别太深

[英]RSPEC and factory girl SystemStackError: stack level too deep

I have trying to solve an issue with my Spec tests and I get the following error Failures: 我试图解决我的Spec测试的问题,我得到以下错误失败:

  1) SessionsController POST 'create' with valid email and password should sign in the user
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/Aurelien/.rvm/gems/ruby-1.9.2-p290@rails3/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:103

Finished in 37.77 seconds
9 examples, 1 failure

Failed examples:

rspec ./spec/controllers/sessions_controller_spec.rb:35 # SessionsController POST 'create' with valid email and password should sign in the user

Prior to this I had problems with associations with my factories. 在此之前,我与工厂的关联存在问题。

Factory.define :role do |role|
  role.name                   "Registered"
  role.association :user, :factory => :user
end

Factory.define :admin do |role|
  role.name                   "Admin"
  role.association :user, :factory => :user
end

Factory.define :user do |user|
  user.first_name             "Foo"
  user.last_name              "Bar"
  user.email                  "foo@bar.com"
  user.password               "foobar"
  user.password_confirmation  "foobar"
  user.status                 "At foobar"
  user.description            "Lorem Ipsum sit dolor amet."
  user.username               "foobar"
  user.association :role, :factory => :role
  user.association :admin, :factory => :role
end

Factory.define :user_with_admin_role, :parent => :user do |user|
  user.after_create { |u| Factory(:role, :user => u) }
end

Factory.define :reg_user do |user|
  user.first_name             "bar"
  user.last_name              "foo"
  user.email                  "bar@foo.com"
  user.password               "foobar"
  user.password_confirmation  "foobar"
  user.status                 "At foobar"
  user.description            "Lorem Ipsum sit dolor amet."
  user.username               "barfoo"
  user.association :role, :factory => :role
end

and my session tests so far are: 到目前为止我的会话测试是:

describe "POST 'create'" do
    describe "invalid signin" do
      before(:each) do
        @attr = { :email => "email@example.org", :password => "invalid" }
      end

      it "should re-render the 'new' page with a flash error" do
        post :create, :session => @attr
        flash.now[:error] =~ /invalid/i
        response.should render_template('new')
      end

    end

    describe "with valid email and password" do

      before(:each) do
        @user = Factory(:user)
        @attr = { :email => @user.email, :password => @user.password}
      end

      it "should sign in the user" do
        post :create, :session => @attr
        controller.current_user.should == @user
      end

    end

  end

I am really not sure what is creating the problem. 我真的不确定是什么造成了这个问题。 In my model I assign a default role to every user as "Registered" and the "Admin" role for the first user. 在我的模型中,我将默认角色分配给每个用户为“已注册”,并为第一个用户分配“管理员”角色。

user.rb user.rb

def assign_default_role
    if User.count == 0
      self.roles << Role.find_by_name("Admin")
      self.roles << Role.find_by_name("Registered")
    end
    self.roles << Role.find_by_name("Registered") unless User.count == 0
  end

Any advise would be most welcome. 任何建议都是最受欢迎的。 Thanks 谢谢

The problem line is this: 问题是这样的:

@user = Factory(:user)

You have a circular reference: your :user factory creates a :role and :admin factory. 您有一个循环引用:您的:user factory创建:role:admin factory。 Then the :role and :admin factories each create another :user factory, which then creates yet another :role and :admin factory, and so on until you get a stack level too deep error. 然后:role:admin工厂分别创建另一个:user factory,然后创建另一个:role:admin factory,依此类推,直到你得到一个堆栈级太深的错误。

You'll need to remove the associations from some of these. 您需要删除其中一些关联。 I'd recommend removing the role.association lines from both :role and :admin . 我建议从:role:admin删除role.association行。 Whenever you create a :user , it will still create the :role and :admin lines for you. 每当您创建一个:user ,它仍会为您创建:role:admin行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM