卡在控制器单元测试错误中

时间:2014-04-26 10:01:05

标签: ruby-on-rails minitest

我在我的控制器上getting this error。相关的第一行如下:

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'

我的控制器看起来很直接:

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

我有两个这个控制器的灯具:

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: 

我不明白为什么minitest正在请求id=0,因为它没有定义,Patientid=0不存在,在灯具上没有这样的东西主控制器。

任何想法如何处理这个?我有大约7个错误,我设法将我的代码修改为1错误。但是我坚持这个。关于在这里做什么,寻找什么的任何线索都没关系。由于我刚开始使用rails,请尽量使用描述。

由于

1 个答案:

答案 0 :(得分:2)

您的预约装置无法接送患者和医生。 patient_id被设置为0,视图中的Patient.find(appointment.patient_id)导致失败。

更改约会夹具,为患者和医生命名,如:

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: