Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test polymorphic controllers in Rails 2.3?
    primarykey
    data
    text
    <p>Dear fellow Overflowers,</p> <p>I will try to give the essence of my issue. Thanks in advance for your patience.</p> <p>My Polymorphic Controller (<code>Appointments</code>) is accessed by two <code>Views</code>, the first belongs to <code>Admin::Doctor</code> namespace and the second one to <code>Patient</code> namespace.</p> <p>the relevant part of the <code>routes.rb</code>:</p> <pre><code>map.resources :patient do |patients| patients.resources :appointments end map.namespace(:admin) do |admin| admin.resources :doctor do |doctors| doctors.resources :appointments end end </code></pre> <p>So, I can now get:</p> <pre><code>patients/:patient_id/appointments admin/doctors/:doctor_id/appointments </code></pre> <p>...and the actual routes:</p> <p><code>rake routes | grep appointment</code></p> <pre><code> new_patient_appointments GET /patients/:patient_id/appointments/new(.:format) {:controller=&gt;"appointments", :action=&gt;"new"} edit_patient_appointments GET /patients/:patient_id/appointments/edit(.:format) {:controller=&gt;"appointments", :action=&gt;"edit"} patient_appointments GET /patients/:patient_id/appointments(.:format) {:controller=&gt;"appointments", :action=&gt;"show"} PUT /patients/:patient_id/appointments(.:format) {:controller=&gt;"appointments", :action=&gt;"update"} DELETE /patients/:patient_id/appointments(.:format) {:controller=&gt;"appointments", :action=&gt;"destroy"} POST /patients/:patient_id/appointments(.:format) {:controller=&gt;"appointments", :action=&gt;"create"} new_admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments/new(.:format) {:controller=&gt;"admin/appointments", :action=&gt;"new"} edit_admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments/edit(.:format){:controller=&gt;"admin/appointments", :action=&gt;"edit"} admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments(.:format) {:controller=&gt;"admin/appointments", :action=&gt;"show"} PUT /admin/doctors/:doctor_id/appointments(.:format) {:controller=&gt;"admin/appointments", :action=&gt;"update"} DELETE /admin/doctors/:doctor_id/appointments(.:format) {:controller=&gt;"admin/appointments", :action=&gt;"destroy"} POST /admin/doctors/:doctor_id/appointments(.:format) {:controller=&gt;"admin/appointments", :action=&gt;"create"} </code></pre> <p>My models:</p> <pre><code>class Patient has_many :appointments, :as =&gt; :attendee end class Doctor has_many :appointments, :as =&gt; :attendee end class Appointment belongs_to :attendee, :polymorphic =&gt; true end </code></pre> <p>My controllers:</p> <p><code>Controllers/Admin/doctors_controller.rb</code></p> <pre><code>class Admin::DoctorsController &lt; AuthorisedController end </code></pre> <p><code>Controllers/appointments_controller.rb</code></p> <pre><code>class AppointmentsController &lt; ApplicationController end </code></pre> <p><code>Controllers/patients_controller.rb</code></p> <pre><code>class PatientsController &lt; ApplicationController end </code></pre> <p>On my <code>test/functional/appointments_controller_test.rb</code>, I am testing for <code>patients</code> without errors but when testing for <code>doctors</code>, I get an <code>ActionController::RoutingError</code>:</p> <pre><code>4) Error: test_should_show_doctor_appointment(AppointmentsControllerTest): ActionController::RoutingError: No route matches {:controller=&gt;"appointments", :id=&gt;"281110143", :action=&gt;"show", :doctor_id=&gt;2} test/functional/appointments_controller_test.rb:55:in `test_should_show_doctor_appointment' </code></pre> <p>EDIT:</p> <p>The relevant part in the tests:</p> <p><code>test/functional/appointments_controller_test.rb</code></p> <pre><code>require 'test_helper' class AppointmentsControllerTest &lt; 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 =&gt; patients(:one).to_param, :appointment_id =&gt; 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 =&gt; doctors(:one).to_param, :appointment_id =&gt; appointments(:app_one).id assert_response :success end end </code></pre> <p>As @Ryan pointed out, the test is under the base namespace, so as a next step, I created a test under <code>Admin</code>.</p> <p><code>test/functional/admin/appointments_controller_test.rb</code></p> <pre><code>class Admin::AppointmentsControllerTest &lt; 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 =&gt; doctors(:one).to_param, :appointment_id =&gt; appointments(:app_one).id assert_response :success end end </code></pre> <p>...and now I get this error:</p> <pre><code> 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' </code></pre> <p>At this point, I added <code>@controller = AppointmentsController.new</code> under the <code>setup</code> method, only to get the very familiar:</p> <pre><code>1) Error: test_should_show_doctor_appointments(Admin::AppointmentsControllerTest): ActionController::RoutingError: No route matches {:action=&gt;"show", :controller=&gt;"appointments", :doctor_id=&gt;2, :id=&gt;"281110143"} test/functional/admin/appointments_controller_test.rb:14:in `test_should_show_doctor_appointments' </code></pre> <p>It seems to me like a vicious circle.</p> <p>EDIT END</p> <p>So, since the test can not find the controller because its route points at <code>admin/appointments</code></p> <ul> <li>why am I able to render <code>/admin/doctors/1/appointments</code> since the <code>appointments_controller.rb</code> does not live neither under <code>Admin</code> folder nor <code>Admin::</code> namespace (but the route points there) and </li> <li>what is the best strategy to write the functional tests for that case?</li> </ul> <p>Thank you in advance!</p> <p>pR</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload