0
votes

I have the following factory girl for create user:

FactoryGirl.define do
 factory :user do
   first_name 'Colin'
   last_name 'Ambler'
   password 'dell13a1'
   address_zip '60657'

   trait :event_operator do
     role Role.find_or_create_by(role_title: "Event Operator", name: "event_operator")
     email "[email protected]"
   end

   trait :athletic_trainer do
     role Role.find_or_create_by(role_title: "Athletic Trainer", name: "athletic_trainer")
     email "[email protected]"
   end

 end
end

As you can see I use the traits for define the role of the user, and set up a different email address.

I have another model called Event, an event belongs to a athletic_trainer that's the name of the association, it just points to the User model, so I use Factory Girl for create the event like this:

FactoryGirl.define do

     factory :event do
       event_type Event.event_types[:tournament]
       sport Event.sports[:lacrosse]
       event_title "NXT Cup"
       event_logo { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'images', 'nxt_cup_logo.png')) }
       gender Event.genders[:boys]
       event_pay_rate 40
       event_participant_numbers 3
       event_feature_photo { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'images', 'nxt_cup_feature.jpg')) }
       event_description "We roll out the red carpet for North America's top youth talent. The nations top youth teams descend on the premier facilities in the Philadelphia region to battle it out and stake claim to one of the summers most highly coveted trophies...the NXT Cup."
       event_operator
     end

    end

as you can see I add the event_operator at the end of the event's factory girl for specify the user association, however this way I do not have access to the user associated in the factory girl.

is it possible to send the athletic_trainer as a parameter to the event's factory girl? I mean I want to create the user using factory girl and then create the event but associate the user I just created, is that possible?

1

1 Answers

1
votes

You can always override fields by passing them to the build or create methods. To set event_operator to your desired user, you would do this:

user = build(:user)
event = build(:event, event_operator: user)