CheeseFry
CheeseFry

Reputation: 1319

MiniTest parent and child from factory in test

I'm getting the following error when running my test.
NoMethodError: undefined method 'departure_date' for nil:NilClass

Here is the test.

  test "successful disbursement should respond with success" do

    post :disbursement, {id: @invoice.invoice_id, trip: attributes_for(:trip)}
    assert_response :success
  end

In the setup portion of the test I'm creating the following.

  setup do
    @controller = Api::V1::InvoicesController.new
    @invoice = create(:invoice)
    @trip = create(:trip)
  end

The trip factory looks like this.

FactoryGirl.define do
  factory :trip do
    depart_airport "MCI"
    arrive_airport "ORD"
    passenger_first_name "Joe"
    passenger_last_name "Business"
    passenger_count 1
    departure_date {10.days.from_now}
    invoice
  end

end

The invoice factory looks like this.

FactoryGirl.define do
  factory :invoice do
    sequence(:invoice_id) { |n| "INV#{n}"}
    merchant
    amount 500.00
    item_count 5
    paid false
    currency "GBP"
    invoice_type "pre-flight"
  end

end

Can't quite figure out how to make sure the invoice has a trip. I'm guessing this is why the test can't find the departure_date it should.

Upvotes: 1

Views: 133

Answers (1)

Anthony To
Anthony To

Reputation: 2303

From what I understand, you are trying to associate the trip and invoice. If my understanding is correct, try this.

setup do
    @controller = Api::V1::InvoicesController.new
    @invoice = create(:invoice)
    @trip = create(:trip, invoice: @invoice)
  end

Upvotes: 1

Related Questions