Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test Controllers under different namespaces and why this test fails?
    primarykey
    data
    text
    <p>Dear fellow Overflowers,</p> <p>I am using Rails 2.3 and I have created a polymorphic Controller which is accessed by Views belonging to different namespaces. Here is the story and thanks for reading it in advance: </p> <p>I have these 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>...these 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>...and these tests:</p> <p>The relevant part in the tests:</p> <p><code>test/functional/appointments_conrtroller_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 after the code block: 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>Error:</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>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 like a vicious circle to me.</p> <p>Thanks anyways...</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.
    1. This table or related slice is empty.
    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