wweare
wweare

Reputation: 241

Why this factory does not work?

I'm trying to unit test my app.I have a order model and this model has a attr_accessor register_client.If accessor has a value 1:

order.client = User.create

it works, but when I trying to test this - I create a factory

FactoryGirl.define do

  factory :order do
    username Faker::Name.name
    register_client "1"
  end

end

and it fails with:

order = FactoryGirl.create(:order)
order.client
=> nil

Upvotes: 0

Views: 39

Answers (1)

apneadiving
apneadiving

Reputation: 115541

You should do:

FactoryGirl.define do

  factory :user do
    #put necessary here
  end

  factory :order do

    trait :with_client do
      register_client "1"
      association :client, factory: :user
    end

    trait :unregistered_client do 
      username { Faker::Name.name }
    end

    factory :order_with_client,  traits: [:with_client]
  end

end

Then you'd have:

FactoryGirl.create(:order, :with_client)
# same as
FactoryGirl.create(:order_with_client)

FactoryGirl.create(:order, :unregistered_client)

Upvotes: 1

Related Questions