How to test polymorphic controllers in Rails 2.3?

2019-08-03 04:19发布

Dear fellow Overflowers,

I will try to give the essence of my issue. Thanks in advance for your patience.

My Polymorphic Controller (Appointments) is accessed by two Views, the first belongs to Admin::Doctor namespace and the second one to Patient namespace.

the relevant part of the 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

So, I can now get:

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

...and the actual routes:

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"}

My models:

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

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

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

My controllers:

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

On my test/functional/appointments_controller_test.rb, I am testing for patients without errors but when testing for doctors, I get an 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'

EDIT:

The relevant part in the tests:

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

As @Ryan pointed out, the test is under the base namespace, so as a next step, I created a test under 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

...and now I get this error:

 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' 

At this point, I added @controller = AppointmentsController.new under the setup method, only to get the very familiar:

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'

It seems to me like a vicious circle.

EDIT END

So, since the test can not find the controller because its route points at admin/appointments

  • why am I able to render /admin/doctors/1/appointments since the appointments_controller.rb does not live neither under Admin folder nor Admin:: namespace (but the route points there) and
  • what is the best strategy to write the functional tests for that case?

Thank you in advance!

pR

1条回答
手持菜刀,她持情操
2楼-- · 2019-08-03 05:01

This error:

ActionController::RoutingError: No route matches {:controller=>"appointments", :id=>"281110143", :action=>"show", :doctor_id=>2}

Shows that you're making a request to a controller called AppointmentsController, but judging by your routes:

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"}

That route is only available within the admin namespace, i.e. Admin::AppointmentsController.

I'm going to bet that you're doing something like describe AppointmentsController rather than describe Admin::AppointmentsController. I don't know for certain because you've not included the critical part that is the test itself.

查看更多
登录 后发表回答