- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
亲爱的 Overflowers,
我会尽量给出我的问题的本质。预先感谢您的耐心等待。
我的多态 Controller (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 路线 | 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 :
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 指出的那样,测试在 base 命名空间下,因此下一步,我在 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'
此时,我在setup
方法下添加了@controller = AppointmentsController.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'
在我看来这是一个恶性循环。
编辑结束
所以,由于测试找不到 Controller ,因为它的路由指向 admin/appointments
/admin/doctors/1/appointments
因为 appointments_controller.rb
不在 Admin
文件夹下也不是 Admin::
命名空间(但路由指向那里)和 提前致谢!
pR
最佳答案
这个错误:
ActionController::RoutingError: No route matches {:controller=>"appointments", :id=>"281110143", :action=>"show", :doctor_id=>2}
显示您正在向名为 AppointmentsController
的 Controller 发出请求,但根据您的路由判断:
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::AppointmentsController
。
我敢打赌,您正在执行类似describe AppointmentsController
的操作,而不是describe Admin::AppointmentsController
。我不确定,因为您没有包括测试本身的关键部分。
关于ruby-on-rails - 如何在 Rails 2.3 中测试多态 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190071/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!