lundi 25 avril 2016

Create Parent and Child with child presence validation Factory Girl

Have a project that has Invoices with many Trips. New story came my way requesting that an Invoice MUST have a trip. I've added a validation validates :trips, presence: true but it is now blowing up a number of my tests since FactoryGirl is trying to save the invoice before creating the associated trip.

FactoryGirl.define do
  factory :invoice do
    sequence(:invoice_id) { SecureRandom.uuid}
    merchant
    amount 100.00
    item_count 1
    paid false
    currency "GBP"
    invoice_type "pre-flight"
    service_rendered false
    cancelled false

    after(:create) { |object| create(:invoice_detail, invoice_id: object.invoice_id)}
    after(:create) { |object| create(:trip, invoice_id: object.invoice_id)}
  end

end

What can I do to create these objects. Preferably at the factory level since there are numerous tests utilizing this behavior (and currently failing because of it.)

Aucun commentaire:

Enregistrer un commentaire