ruby-on-rails - 如何测试不同命名空间下的 Controller 以及为什么此测试失败?

标签 ruby-on-rails testing polymorphic-associations

亲爱的溢出者们,

我正在使用 Rails 2.3,并且创建了一个多态 Controller ,该 Controller 由属于不同 namespace 的 View 访问。这是这个故事,感谢您提前阅读:

我有这些路线:

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"}

...这些 Controller :

Controllers/Admin/doctors_controller.rb

class Admin::DoctorsController < AuthorisedController
end

Controllers/appointments_controller.rb

class AppointmentsController < ApplicationController
end

Controller /患者_controller.rb

class PatientsController < ApplicationController
end

...以及这些测试:

测试中的相关部分:

测试/功能/appointments_conrtroller_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 after the code block:

  test "should show doctor appointment" do
    get :show, :id => doctors(:one).to_param, :appointment_id => appointments(:app_one).id
    assert_response :success
  end

end

错误:

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'

测试位于基本命名空间下,因此下一步,我在 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'

对我来说这似乎是一个恶性循环。

无论如何,谢谢...

pR

最佳答案

你可能应该这样做:

@controller = Admin::AppointmentsController.new 

否则,您将引用主命名空间内的 Controller ,而不是 Admin 命名空间。

关于ruby-on-rails - 如何测试不同命名空间下的 Controller 以及为什么此测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18208722/

相关文章:

ruby-on-rails - 在 Ruby on Rails 上,如何在另一个功能测试中断时运行一个功能测试?

c++ - 具有类型转换的多态复制构造函数

ruby-on-rails - Google App Engine + Ruby on rails + Redis + actioncable

ruby-on-rails - rails : how to create Time object in specific time zone

windows - 是否有必要在所有 Windows 操作系统及其所有版本上测试我的应用程序?

testing - 获取日志为提供了以下所需功能,但 Appium 无法识别

ruby-on-rails - Ruby:Declarative_authorization 多态关联

mySQL - 多个表的外键?

javascript - 如何使用 JQuery 和 Rails 创建类似 Wufoo(表单生成器)的 Web 应用程序?

ruby-on-rails - 如何更改 Redmine 3.x 中的默认登录页面?