亲爱的同学Overflowers,
我会尽量给我的问题的实质。 在此先感谢您的耐心。
我的多态控制器( Appointments
)由两个访问Views
,第一个属于Admin::Doctor
的命名空间,第二个到Patient
的命名空间。
在相关部分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.rb
不活的既不是在Admin
文件夹中,也没有Admin::
命名空间(但路线点那里)和 - 什么是写这种情况下,功能测试的最佳策略是什么?
先感谢您!
的pR