【发布时间】: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