Ruby on rails 如何在Rails 2.3中测试多态控制器?
亲爱的飞越者们 我将尽力说明我的问题的实质。提前感谢您的耐心等待 我的多态控制器(Ruby on rails 如何在Rails 2.3中测试多态控制器?,ruby-on-rails,testing,polymorphic-associations,Ruby On Rails,Testing,Polymorphic Associations,亲爱的飞越者们 我将尽力说明我的问题的实质。提前感谢您的耐心等待 我的多态控制器(预约)由两个视图访问,第一个视图属于管理::医生命名空间,第二个视图属于患者命名空间 routes.rb的相关部分: map.resources :patient do |patients| patients.resources :appointments end map.namespace(:admin) do |admin| admin.resources :doctor do |doctors|
预约
)由两个视图访问,第一个视图属于管理::医生
命名空间,第二个视图属于患者
命名空间
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路由| grep预约
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
我的控制器:
controller/Admin/doctors\u controller.rb
class Admin::DoctorsController < AuthorisedController
end
class AppointmentsController < ApplicationController
end
class PatientsController < ApplicationController
end
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
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
controller/patients\u controller.rb
class Admin::DoctorsController < AuthorisedController
end
class AppointmentsController < ApplicationController
end
class PatientsController < ApplicationController
end
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
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
编辑:
测试中的相关部分:
test/functional/appointment\u controller\u test.rb
class Admin::DoctorsController < AuthorisedController
end
class AppointmentsController < ApplicationController
end
class PatientsController < ApplicationController
end
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
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'
ActionController::RoutingError: No route matches {:controller=>"appointments", :id=>"281110143", :action=>"show", :doctor_id=>2}
此时,我在setup
方法下添加了@controller=AppointsController.new
,只是为了得到非常熟悉的:
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/appointment
- 为什么我能够呈现
/admin/doctors/1/appointments
,因为appointments\u controller.rb
既不在admin
文件夹下,也不在admin::
命名空间下(但路由点在那里)和
- 为这种情况编写功能测试的最佳策略是什么
提前谢谢你
pR此错误:
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'
ActionController::RoutingError: No route matches {:controller=>"appointments", :id=>"281110143", :action=>"show", :doctor_id=>2}
显示您正在向名为AppointsController
的控制器发出请求,但根据您的路线判断:
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"}
该路由仅在管理员命名空间中可用,即admin::AppointsController
我敢打赌,您正在做类似于descripe appoints controller
的事情,而不是descripe Admin::appoints controller
。我不确定,因为您没有包括测试本身的关键部分。此错误:
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'
ActionController::RoutingError: No route matches {:controller=>"appointments", :id=>"281110143", :action=>"show", :doctor_id=>2}
显示您正在向名为AppointsController
的控制器发出请求,但根据您的路线判断:
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"}
该路由仅在管理员命名空间中可用,即admin::AppointsController
我敢打赌,您正在做类似于descripe appoints controller
的事情,而不是descripe Admin::appoints controller
。我不确定,因为您没有包括测试本身的关键部分。运行命令gem install rails-v 4.0.0
,然后重试=DSo,Rails 4.0.0解决了这个问题吗?我对Rails 2.3.18中的多态控制器了解不多,不幸的是,在Rails版本中,一些像我这样的年轻程序员甚至在它流行的时候都没有编程。很难找到对像这样的旧版本的支持,如果您可以升级,我建议您使用。好吧,在任何情况下都不难:运行命令gem install rails-v 4.0.0
,然后重试=DSo,Rails 4.0.0解决了这个问题吗?我对Rails 2.3.18中的多态控制器了解不多,不幸的是,在Rails版本中,一些像我这样的年轻程序员甚至在它流行的时候都没有编程。很难找到像这样的旧版本的支持,如果你能升级,我会推荐它。好吧,在任何情况下都没那么难:嘿,Ryan,谢谢你花时间!您在名称空间范围上是正确的,并且更进一步了,但是我仍然得到错误。我已经添加了相关部分。@Cacofonix:请为这个新问题打开一个新问题,而不是修改现有问题。这已在Hey Ryan完成,感谢您花时间!您在名称空间范围上是正确的,并且更进一步了,但是我仍然得到错误。我已经添加了相关部分。@Cacofonix:请为这个新问题打开一个新问题,而不是修改现有问题。这已在