vendredi 29 janvier 2016

Factory_girl how to create HABTM association using traits declared for child?

How does one create a HABTM relationship using factory_girl using traits declared for the child?

This is basically what I am trying to do:

factory :site do
  name 'some domain name'

  trait :rejected do
    rejected true
  end

  trait :processing do
    processing true
  end
end

factory :user do
  trait :bob do
    name 'Bob'

    after(:create) do |user, evaluator|
      create_list(:rejected, 1, users: [user])
      create_list(:processing, 1, users: [user])
    end
  end
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :sites
end

I have been searching for examples like above, but everything I have found so far only has the trait in the parent and just uses the child factory. When I try to reference the child trait, I get a Factory not registered error.

In addition, the list is not going to change. I don't need this to be dynamic. I know what sites I want to give to each user.

Aucun commentaire:

Enregistrer un commentaire