님께,Rails 2.3에서 다형성 컨트롤러를 테스트하는 방법은 무엇입니까?
안녕하세요 저는 제 문제의 본질을 나타내려고합니다. 양해 해 주셔서 미리 감사드립니다.
내 다형성은 제어기 (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
test/functional/appointments_controller_test.rb
에 74,
, 나는 오류없이 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'
, 나는 단지 잘 알고 얻으려면, 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'
악순환처럼 보입니다.
편집 END Admin
폴더 아래에 둘 살고하지 않는 appointments_controller.rb
이후 /admin/doctors/1/appointments
을 렌더링 내가 수 있어요 왜 테스트가 admin/appointments
- 에서의 경로 점 때문에 컨트롤러를 찾을 수 없기 때문에 그래서
,도
Admin::
네임 스페이스 (경로 지점을 가리킴) 및 - 이 경우의 기능 테스트를 작성하는 가장 좋은 전략은 무엇입니까?
감사합니다.
홍보
'gem install rails -v 4.0.0' 명령을 실행하고 다시 시도하십시오. = D – OneChillDude
그래서 Rails 4.0.0이 문제를 해결합니까? – Cacofonix
글쎄, 필자는 레일즈 2.3.18의 다형성 컨트롤러에 대해 잘 모르고 불행히도 그 레일 버전에서 나 자신과 같은 젊은 프로그래머 중 일부는 현재의 프로그래밍이 아니었다. 그런 오래된 버전에 대한 지원을 찾으려고 노력하는 것이 어렵습니다. 업그레이드가 가능하다면 추천 할 것입니다. – OneChillDude