0

您好,感谢您的阅读。这是我的工厂:

FactoryGirl.define do
  factory :venue do
    sequence(:name) { |n| "ExampleVenue#{n}" }

    factory :venue_with_gig do
      after_create do |venue|
        create(:gig, venue: venue)               #I've created a valid gig factory as well
      end                                        
    end
  end
end 

场地有_许多演出和演出属于_场地。我在这里所拥有的似乎遵循了此处的思想机器人博客文章中的示例

然而,我运行我的模型规格并由于wrong Number of Arguments (3 for 2) at the以下操作之前的错误而失败:

需要'spec_helper'

describe Venue do
  describe "basic attributes" do
    before { @venue = create(:venue_with_gig) }

    subject { @venue }

    it { should be_valid }
    it { should respond_to(:name) }
    # it { should respond_to(:address) }
    it { should respond_to(:gigs) }
  end 

  describe "with invalid params" do
    before { @venue = build(:venue, name: " ") }
    subject { @venue }

    it { should_not be_valid }
  end
end

我已经看它太久了,可以使用一个新的视角。谢谢!

4

1 回答 1

1

由于场地可能有演出,所以有必要在说“venue_with_gig”时定义你需要多少人演出,更好的名字是“venue_with_gigs”。你还需要使用create_list

# under venue define as your code
factory :venue_with_gigs do
  ignore do
    gigs_count 3 # Let's say 3 gigs as default
  end

  after(:create) do |venue, evaluator|
    FactoryGirl.create_list(:gig, evaluator.gigs_count, venue: venue)
  end                                        
end

默认使用

FactoryGirl.create(:venue_with_gigs)
# => 1 venue and 3 gigs created. Gigs under the venue

定制数量

FactoryGirl.create(:venue_with_gigs, gigs_count: 10)
# => 1 venue 10 gigs created
于 2013-06-16T18:13:21.360 回答