1

親愛なるオーバーフローの諸君、

私の問題の本質を伝えようとします。お待ちいただきありがとうございます。

My Polymorphic Controller ( Appointments) は 2 つの によってアクセスされます。1 つ目は名前空間にViews属し、2 つ目は名前空間に属します。Admin::DoctorPatient

の関連部分routes.rb:

map.resources :patient do |patients|
  patients.resources :appointments
end

map.namespace(:admin) do |admin|
  admin.resources :doctor do |doctors|
    doctors.resources :appointments
  end
end

だから、私は今得ることができます:

patients/:patient_id/appointments
admin/doctors/:doctor_id/appointments

...そして実際のルート:

rake routes | grep appointment

 new_patient_appointments GET    /patients/:patient_id/appointments/new(.:format)    {:controller=>"appointments", :action=>"new"}
edit_patient_appointments GET    /patients/:patient_id/appointments/edit(.:format)  {:controller=>"appointments", :action=>"edit"}
     patient_appointments GET    /patients/:patient_id/appointments(.:format)            {:controller=>"appointments", :action=>"show"}
                          PUT    /patients/:patient_id/appointments(.:format)            {:controller=>"appointments", :action=>"update"}
                          DELETE /patients/:patient_id/appointments(.:format)   {:controller=>"appointments", :action=>"destroy"}
                          POST   /patients/:patient_id/appointments(.:format)   {:controller=>"appointments", :action=>"create"}

 new_admin_doctor_appointments GET    /admin/doctors/:doctor_id/appointments/new(.:format) {:controller=>"admin/appointments", :action=>"new"}
edit_admin_doctor_appointments GET    /admin/doctors/:doctor_id/appointments/edit(.:format){:controller=>"admin/appointments", :action=>"edit"}
     admin_doctor_appointments GET    /admin/doctors/:doctor_id/appointments(.:format)     {:controller=>"admin/appointments", :action=>"show"}
                               PUT    /admin/doctors/:doctor_id/appointments(.:format)    {:controller=>"admin/appointments", :action=>"update"}
                               DELETE /admin/doctors/:doctor_id/appointments(.:format)   {:controller=>"admin/appointments", :action=>"destroy"}
                               POST   /admin/doctors/:doctor_id/appointments(.:format)   {:controller=>"admin/appointments", :action=>"create"}

私のモデル:

class Patient
  has_many :appointments, :as => :attendee
end

class Doctor
  has_many :appointments, :as => :attendee
end

class Appointment
  belongs_to :attendee, :polymorphic => true
end

私のコントローラー:

Controllers/Admin/doctors_controller.rb

class Admin::DoctorsController < AuthorisedController
end

Controllers/appointments_controller.rb

class AppointmentsController < ApplicationController
end

Controllers/patients_controller.rb

class PatientsController < ApplicationController
end

私のtest/functional/appointments_controller_test.rbでは、patientsエラーなしでテストしていますが、 をテストするとdoctors、次のようになりますActionController::RoutingError

4) Error:
test_should_show_doctor_appointment(AppointmentsControllerTest):
ActionController::RoutingError: No route matches {:controller=>"appointments", :id=>"281110143", :action=>"show", :doctor_id=>2}
test/functional/appointments_controller_test.rb:55:in `test_should_show_doctor_appointment'

編集:

テストの関連部分:

test/functional/appointments_controller_test.rb

require 'test_helper'

class AppointmentsControllerTest < ActionController::TestCase

  fixtures :patients, :appointments, :doctors, :users
  # The following passes:

  def setup
    login_as :admin
  end

  test "should show patient appointment" do
    get :show, :id => patients(:one).to_param, :appointment_id => appointments(:app_one).id
    assert_response :success
  end

  # The following fails, giving the error mentioned above:

  test "should show doctor appointment" do
    get :show, :id => doctors(:one).to_param, :appointment_id => appointments(:app_one).id
    assert_response :success
  end

end

@Ryan が指摘したように、テストはベース名前空間の下にあるため、次のステップとして、Admin.

test/functional/admin/appointments_controller_test.rb

class Admin::AppointmentsControllerTest < ActionController::TestCase

  fixtures :patients, :appointments, :doctors, :users
  # The following passes:

  def setup
    login_as :admin
  end

  test "should show doctor appointment" do
    get :show, :id => doctors(:one).to_param, :appointment_id => appointments(:app_one).id
    assert_response :success
  end

end

...そして今、私はこのエラーを受け取ります:

 1) Error:
 test_should_show_doctor_appointment(Admin::AppointmentsControllerTest):
 RuntimeError: @controller is nil: make sure you set it in your test's setup method.
 test/functional/admin/appointments_controller_test.rb:13:in `test_should_show_doctor_appointment' 

この時点@controller = AppointmentsController.newで、メソッドの下に追加しましsetupたが、非常によく知られているものを取得するためだけです:

1) Error:
test_should_show_doctor_appointments(Admin::AppointmentsControllerTest):
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"appointments", :doctor_id=>2, :id=>"281110143"}
test/functional/admin/appointments_controller_test.rb:14:in `test_should_show_doctor_appointments'

私には悪循環のように思えます。

編集終了

そのため、テストではコントローラーが見つからないため、そのルートはadmin/appointments

  • はフォルダーにも名前空間にも存在しない/admin/doctors/1/appointmentsため(ただし、ルートはそこを指している)、レンダリングできるのはなぜですか?appointments_controller.rbAdminAdmin::
  • その場合の機能テストを作成するための最良の戦略は何ですか?

前もって感謝します!

PRR

4

1 に答える 1