如何在不同的命名空间下测试控制器以及为什么此测试失败? [英] How to test Controllers under different namespaces and why this test fails?

查看:53
本文介绍了如何在不同的命名空间下测试控制器以及为什么此测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的溢出者,

我正在使用 Rails 2.3 并且我创建了一个多态控制器,它可以被属于不同命名空间的视图访问.这是故事,感谢您提前阅读:

I am using Rails 2.3 and I have created a polymorphic Controller which is accessed by Views belonging to different namespaces. Here is the story and thanks for reading it in advance:

我有这些路线:

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

...这些控制器:

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_conrroller_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 下创建了一个测试.

the test is under the base namespace, so as a next step, I created a test under 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

...现在我收到此错误:

...and now I get this error:

 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,只是为了得到非常熟悉的:

At this point, I added @controller = AppointmentsController.new under the setup method, only to get the very familiar:

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'

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

It seems like a vicious circle to me.

还是谢谢...

pR

推荐答案

你可能应该这样做:

@controller = Admin::AppointmentsController.new 

否则,您将引用主命名空间内的控制器,而不是 Admin 命名空间.

Otherwise you're referencing the controller inside the main namespace and not the Admin namespace.

这篇关于如何在不同的命名空间下测试控制器以及为什么此测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆