Ruby on rails 无法测试嵌套的Rails控制器

Ruby on rails 无法测试嵌套的Rails控制器,ruby-on-rails,rspec,controller,Ruby On Rails,Rspec,Controller,如何测试任务::小时控制器? 来自我的rspec测试的请求点击HoursController,而不是任务::HoursController 路线 #POST/tasks/:task_id/hours(:format)tasks/hours#create 测试 describe Tasks::HoursController, :type => :controller do it "assigns all hours as @hours" do hour = Hour.cre

如何测试任务::小时控制器? 来自我的rspec测试的请求点击HoursController,而不是任务::HoursController

路线

#POST/tasks/:task_id/hours(:format)tasks/hours#create

测试

describe Tasks::HoursController, :type => :controller do

  it "assigns all hours as @hours" do
      hour = Hour.create! valid_attributes
      get  :index, { task_id:  @task.id }, valid_session
      assigns(:hours).should eq([hour])
  end
end
Processing by HoursController#index as HTML
  Parameters: {"task_id"=>"207"}
Completed 500 Internal Server Error in 3ms
日志输出

describe Tasks::HoursController, :type => :controller do

  it "assigns all hours as @hours" do
      hour = Hour.create! valid_attributes
      get  :index, { task_id:  @task.id }, valid_session
      assigns(:hours).should eq([hour])
  end
end
Processing by HoursController#index as HTML
  Parameters: {"task_id"=>"207"}
Completed 500 Internal Server Error in 3ms

我想您需要删除Tasks名称空间(还要注意,在您的描述中,您使用了单数和复数代码)

并将descripe块放入具有任务命名空间的模块中

module Tasks
  describe HoursController do
    it "assigns all hours as @hours" do
        hour = Hour.create! valid_attributes
        get  :index, { task_id:  @task.id }, valid_session
        assigns(:hours).should eq([hour])
    end
  end
end

你们很难看到,因为我没有把代码发到我的控制器上

解决办法是:

-class Tasks::HoursSpentController < ApplicationController
+module Tasks
+  class HoursSpentsController < ApplicationController
-class Tasks::HoursSpentController
还有@vince-v发布的建议。 谢谢你,文斯。:-)