web-dev-qa-db-ja.com

has_manyアソシエーションを使用してFactoryGirlでファクトリをセットアップする方法

セットアップを間違った方法で行っている場合、誰かに教えてもらえますか?

Has_many.throughアソシエーションを持つ次のモデルがあります。

class Listing < ActiveRecord::Base
  attr_accessible ... 

  has_many :listing_features
  has_many :features, :through => :listing_features

  validates_presence_of ...
  ...  
end


class Feature < ActiveRecord::Base
  attr_accessible ...

  validates_presence_of ...
  validates_uniqueness_of ...

  has_many :listing_features
  has_many :listings, :through => :listing_features
end


class ListingFeature < ActiveRecord::Base
  attr_accessible :feature_id, :listing_id

  belongs_to :feature  
  belongs_to :listing
end

私はRails 3.1.rc4、FactoryGirl 2.0.2、factory_girl_Rails 1.1.0、およびrspecを使用しています。これは、:listingファクトリーの基本的なrspec rspec健全性チェックです。

it "creates a valid listing from factory" do
  Factory(:listing).should be_valid
end

こちらはFactory(:listing)です

FactoryGirl.define do
  factory :listing do
    headline    'headline'
    home_desc   'this is the home description'
    association :user, :factory => :user
    association :layout, :factory => :layout
    association :features, :factory => :feature
  end
end

:listing_featureおよび:featureファクトリーも同様にセットアップされます。
association :features行がコメント化されている場合、すべてのテストに合格します。

association :features, :factory => :feature

エラーメッセージはundefined method 'each' for #<Feature>です。これは、listing.featuresが配列を返すため、私にとって理にかなっていると思いました。だから私はそれを

association :features, [:factory => :feature]

そして今私が得るエラーはArgumentError: Not registered: featuresです。このようにファクトリオブジェクトを生成することは賢明ではないのですか、それとも私は何が欠けていますか?ありとあらゆる入力に感謝します!

84
Tonys

これらの種類の関連付けを作成するには、FactoryGirlのコールバックを使用する必要があります。

ここに完璧な例があります。

https://thoughtbot.com/blog/aint-no-calla-back-girl

あなたの例にそれを持ち帰る。

Factory.define :listing_with_features, :parent => :listing do |listing|
  listing.after_create { |l| Factory(:feature, :listing => l)  }
  #or some for loop to generate X features
end
54
winfred

または、ブロックを使用して、associationキーワードをスキップできます。これにより、データベースに保存せずにオブジェクトを構築することができます(そうしないと、buildの代わりにcreate関数を使用しても、has_many関連付けによりレコードがdbに保存されます)。

FactoryGirl.define do
  factory :listing_with_features, :parent => :listing do |listing|
    features { build_list :feature, 3 }
  end
end
101
JellicleCat

traitを使用できます:

FactoryGirl.define do
  factory :listing do
    ...

    trait :with_features do
      features { build_list :feature, 3 }
    end
  end
end

callbackを使用して、DBの作成が必要な場合:

...

trait :with_features do
  after(:create) do |listing|
    create_list(:feature, 3, listing: listing)
  end
end

次のように仕様で使用します。

let(:listing) { create(:listing, :with_features) }

これにより、工場内の重複が削除され、より再利用可能になります。

https://robots.thoughtbot.com/remove-duplication-with-factorygirls-traits

20
ehoffmann

私はいくつかの異なるアプローチを試しましたが、これは私にとって最も確実に機能したものです(あなたのケースに合わせて)

FactoryGirl.define do
  factory :user do
    # some details
  end

  factory :layout do
    # some details
  end

  factory :feature do
    # some details
  end

  factory :listing do
    headline    'headline'
    home_desc   'this is the home description'
    association :user, factory: :user
    association :layout, factory: :layout
    after(:create) do |liztng|
      FactoryGirl.create_list(:feature, 1, listing: liztng)
    end
  end
end
20
Dave Sag

ここに私が私を設定する方法があります:

# Model 1 PreferenceSet
class PreferenceSet < ActiveRecord::Base
  belongs_to :user
  has_many :preferences, dependent: :destroy
end

#Model 2 Preference

class Preference < ActiveRecord::Base    
  belongs_to :preference_set
end



# factories/preference_set.rb

FactoryGirl.define do
  factory :preference_set do
    user factory: :user
    filter_name "market, filter_structure"

    factory :preference_set_with_preferences do
      after(:create) do |preference|
        create(:preference, preference_set: preference)
        create(:filter_structure_preference, preference_set: preference)
      end
    end
  end

end

# factories/preference.rb

FactoryGirl.define do
  factory :preference do |p|
    filter_name "market"
    filter_value "12"
  end

  factory :filter_structure_preference, parent: :preference do
    filter_name "structure"
    filter_value "7"
  end
end

そして、あなたのテストでは次のことができます:

@preference_set = FactoryGirl.create(:preference_set_with_preferences)

お役に立てば幸いです。

0
rii