ruby-on-rails – RSPEC和工厂女孩SystemStackError:堆栈级别太深

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – RSPEC和工厂女孩SystemStackError:堆栈级别太深前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图解决我的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

在此之前,我遇到了与我的工厂联系的问题.

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

我的会话测试到目前为止是:

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

我真的不知道创建问题是什么.
在我的模型中,我为每个用户分配默认角色为“注册”和第一个用户的“管理员”角色.

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

任何建议将是最受欢迎的.
谢谢

解决方法

问题是这样的:
@user = Factory(:user)

您有一个循环引用:您的用户工厂创建一个:角色和:admin工厂.然后:角色和:管理工厂每个创建另一个:用户工厂,然后创建另一个:角色和:管理工厂,等等,直到你得到一个堆栈级别太深的错误.

您需要从其中的一些删除关联.我建议从role和:admin中删除role.association行.无论何时创建:用户,它仍将为您创建:角色和:admin行.

原文链接:https://www.f2er.com/ruby/266557.html

猜你在找的Ruby相关文章