Question

I am getting this error on my controller. The relevant first lines are the following:

Started at 2014-04-26 11:29:45 +0200 w/ seed 13696.                                                                                                                        [82/97]

AppointmentsControllerTest
              PASS (0:00:00.088) test_should_create_appointment
              PASS (0:00:00.097) test_should_destroy_appointment
              PASS (0:00:00.141) test_should_get_edit
             ERROR (0:00:00.151) test_should_get_index
          Couldn't find Patient with id=0
        @ /Users/atma/.rvm/gems/ruby-2.1.0@rails/gems/activerecord-4.0.3/lib/active_record/relation/finder_methods.rb:198:in `raise_record_not_found_exception!'
          /Users/atma/.rvm/gems/ruby-2.1.0@rails/gems/activerecord-4.0.3/lib/active_record/relation/finder_methods.rb:284:in `find_one'
          /Users/atma/.rvm/gems/ruby-2.1.0@rails/gems/activerecord-4.0.3/lib/active_record/relation/finder_methods.rb:268:in `find_with_ids'
          /Users/atma/.rvm/gems/ruby-2.1.0@rails/gems/activerecord-4.0.3/lib/active_record/relation/finder_methods.rb:35:in `find'
          /Users/atma/.rvm/gems/ruby-2.1.0@rails/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:122:in `find'
          /Users/atma/.rvm/gems/ruby-2.1.0@rails/gems/activerecord-4.0.3/lib/active_record/querying.rb:3:in `find'

My controller looks pretty much straight forward:

require 'test_helper'

class AppointmentsControllerTest < ActionController::TestCase
  setup do
    @appointment = appointments(:one)
    @update = {
      patient_id: Patient.last.id,
      doctor_id: Doctor.last.id,
      date: "2014-05-01 10:24",
      status: 'Επικυρωμένο',
      notes: 'Σχόλια'
    }
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:appointments)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create appointment" do
    assert_difference('Appointment.count') do
      post :create, appointment: @update
    end

    assert_redirected_to appointment_path(assigns(:appointment))
  end

  test "should show appointment" do
    get :show, id: @appointment
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @appointment
    assert_response :success
  end

  test "should update appointment" do
    patch :update, id: @appointment, appointment: @update
    assert_redirected_to appointment_path(assigns(:appointment))
  end

  test "should destroy appointment" do
    assert_difference('Appointment.count', -1) do
      delete :destroy, id: @appointment
    end

    assert_redirected_to appointments_path
  end
end

I have two fixtures for this controller:

one:
  patient_id: Patient.last.id
  doctor_id: Doctor.first.id
  date: "2015-01-01 10:20"
  status: 'Επικυρωμένο'
  notes: 'Ο ασθενής θα έρθει από άλλη πόλη'

two:
  patient_id: Patient.first.id
  doctor_id: Doctor.last.id
  date: "2015-02-01 11:20"
  status: 'Ακυρωμένο'
  notes: 

I don't understand why minitest is requesting id=0 since it's not defined, the Patient with id=0 does not exist, there's nothing like this on fixtures or in the main controller.

Any ideas how to deal with this? I had about 7 errors, I managed to fix my code down to 1 error. But I'm stuck with this one. Any clues on what to do here, what to look for, would be fine. Since I'm new to rails please be as descriptive as possible.

Thanks

Was it helpful?

Solution

Your appointments fixtures are not able to pick up the patient and doctor. The patient_id is being set to 0, and it is Patient.find(appointment.patient_id) in the view that is causing the failure.

Change your appointments fixture to name your patient and doctor like:

one:
  patient: lelou
  doctor: alex
  date: "2015-01-01 10:20"
  status: 'Επικυρωμένο'
  notes: 'Ο ασθενής θα έρθει από άλλη πόλη'

two:
  patient: koutos
  doctor: pan
  date: "2015-02-01 11:20"
  status: 'Ακυρωμένο'
  notes: 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top