How to create has_and_belongs_to_many associations in Factory girl

前端 未结 11 605
生来不讨喜
生来不讨喜 2020-12-07 09:27

Given the following

class User < ActiveRecord::Base
  has_and_belongs_to_many :companies
end

class Company < ActiveRecord::Base
  has_and_belongs_to_m         


        
相关标签:
11条回答
  • 2020-12-07 09:41

    For HABTM I used traits and callbacks.

    Say you have the following models:

    class Catalog < ApplicationRecord
      has_and_belongs_to_many :courses
      …
    end
    class Course < ApplicationRecord
      …
    end
    

    You can define the Factory above:

    FactoryBot.define do
      factory :catalog do
        description "Catalog description"
        …
    
        trait :with_courses do
          after :create do |catalog|
            courses = FactoryBot.create_list :course, 2
    
            catalog.courses << courses
            catalog.save
          end
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-07 09:42
      factory :company_with_users, parent: :company do
    
        ignore do
          users_count 20
        end
    
        after_create do |company, evaluator|
          FactoryGirl.create_list(:user, evaluator.users_count, users: [user])
        end
    
      end
    

    Warning: Change users: [user] to :users => [user] for ruby 1.8.x

    0 讨论(0)
  • 2020-12-07 09:44

    Factorygirl has since been updated and now includes callbacks to solve this problem. Take a look at http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl for more info.

    0 讨论(0)
  • 2020-12-07 09:48

    Here is the solution that works for me.

    FactoryGirl.define do
    
      factory :company do
        #company attributes
      end
    
      factory :user do
       companies {[FactoryGirl.create(:company)]}
       #user attributes
      end
    
    end
    

    if you will need specific company you can use factory this way

    company = FactoryGirl.create(:company, #{company attributes})
    user = FactoryGirl.create(:user, :companies => [company])
    

    Hope this will be helpful for somebody.

    0 讨论(0)
  • 2020-12-07 09:48

    I couldn´t find an example for the above mentioned case on the provided website. (Only 1:N and polymorphic assocations, but no habtm). I had a similar case and my code looks like this:

    Factory.define :user do |user|
     user.name "Foo Bar"
     user.after_create { |u| Factory(:company, :users => [u]) }
    end
    
    Factory.define :company do |c|
     c.name "Acme"
    end
    
    0 讨论(0)
  • 2020-12-07 09:50

    In my opinion, Just create two different factories like:

     Factory.define :user, :class => User do |u|
      # Just normal attributes initialization
     end
    
     Factory.define :company, :class => Company do |u|
      # Just normal attributes initialization
     end
    

    When you write the test-cases for user then just write like this

     Factory(:user, :companies => [Factory(:company)])
    

    Hope it will work.

    0 讨论(0)
提交回复
热议问题