【问题标题】:Rails SystemStackError: stack level too deepRails SystemStackError:堆栈级别太深
【发布时间】:2017-08-19 10:59:03
【问题描述】:

我正在使用 rspec capybara 和 factory girl 测试我的应用程序 (Rails 5) 我有以下错误... 我不确定发生了什么...我对 rspec 很陌生,希望您能帮助我:) 谢谢

Randomized with seed 41137

An error occurred in a `before(:suite)` hook.
Failure/Error: FactoryGirl.lint

SystemStackError:
  stack level too deep

你会在下面找到我的代码:

工厂.rb

FactoryGirl.define do
  factory :event do
    name {Faker::Friends.character}
    total_price 50
    participant
  end

  factory :participant do
    first_name { Faker::Name.first_name }
    salary 900
    event
  end
end

事件.rb

class Event < ApplicationRecord
  has_many  :participants, inverse_of: :event
  validates :participants, presence: true
  validates :name, presence: true, length: {minimum: 2}
  validates :total_price, presence: true


  accepts_nested_attributes_for :participants, reject_if: :all_blank, allow_destroy: true

  def total_salary
    all_salary = []
    participants.each do |participant|
      all_salary << participant.salary
    end
    return @total_salary = all_salary.inject(0,:+)
  end
end

event_spec.rb

require 'rails_helper'

describe Event do
  it { should have_many(:participants) }
  it { should validate_presence_of(:participants) }
  it { should validate_presence_of(:name) }
  it { should validate_presence_of(:total_price) }

  describe "#total_salary" do
    it "should return the total salary of the participants" do
      partcipant_1 = create(:participant, salary: 2000)
      partcipant_2 = create(:participant, salary: 3000)

      expect(partcipant_1.salary + partcipant_2.salary).to eq(5000)
    end
  end
end

编辑

在我的参与者模型中,我必须添加 optional: true belongs_to :event, option: true

所以 fabriciofreitag 建议效果很好:)

【问题讨论】:

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


    【解决方案1】:

    让我们看看你的工厂:

    FactoryGirl.define do
      factory :event do
        name {Faker::Friends.character}
        total_price 50
        participant
      end
      factory :participant do
        first_name { Faker::Name.first_name }
        salary 900
        event
      end
    end
    

    在这种情况下,事件的创建将创建一个参与者,该参与者将创建一个事件,该参与者将创建一个参与者。以此类推,在无限循环中(堆栈级别太深)。

    也许你可以把它改成这样:

    FactoryGirl.define do
      factory :event do
        name {Faker::Friends.character}
        total_price 50
        participants { create_list(:participant, 3, event: self) }
      end
      factory :participant do
        first_name { Faker::Name.first_name }
        salary 900
      end
    end
    

    【讨论】:

    • 您好感谢您的建议不幸的是我有这个错误:.rvm/gems/ruby-2.3.3/gems/factory_girl-4.8.0/lib/factory_girl/definition_proxy.rb:42:in add_attribute': wrong number of arguments (given 3, expected 1..2) (ArgumentError)
    • 抱歉,我更新了帖子并进行了更正。@Liwis
    • 您好,谢谢 :) 我仍然有错误 :( Failure/Error: FactoryGirl.lint FactoryGirl::InvalidFactoryError: The following factories are invalid: * event - Validation failed: Event must exist (ActiveRecord::RecordInvalid) * participant - Validation failed: Event must exist (ActiveRecord::RecordInvalid)
    • @Liwis 道歉,很难测试我现在的位置,但最后一个版本应该可以解决它。你没有发布参与者模型,所以我不知道它需要事件。
    • @Liwis,不要只是复制/粘贴您遇到的错误。付出一些努力去理解它们,如果你不这样做,那就问这个问题。似乎您希望 Fabricio 为您编写代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2013-10-05
    • 2014-10-25
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多