我曾多次试图进行测试,你认为我现在已经把它弄清楚了 - 我没有。
最新的方法是使用minitest - 至少它主要是Ruby而不是一堆DSL正在学习另一种语言,似乎有数百种方法。现在我可能已经用我最新的实验挖了一个洞,但我有点像我想要完成的。它是嵌套资源的组合,包括一些多态资源。它也使用Pundit进行授权。
模型定义了一个分层实体(想想公司或政府机构,例如森林服务,或者在我的情况下是VFW层次结构)
基本型号class Department < ActiveRecord::Base
has_many :districts, dependent: :destroy
has_many :posts, through: :districts
has_many :reports, through: :posts
has_many :officers, as: :fillable, dependent: :destroy
has_many :markups, as: :markupable, dependent: :destroy
has_many :users, as: :loginable, dependent: :destroy
end
class District < ActiveRecord::Base
belongs_to :department
has_many :posts, dependent: :destroy
has_many :reports, through: :posts
has_many :officers, as: :fillable, dependent: :destroy
has_many :markups, as: :markupable, dependent: :destroy
has_many :users, as: :loginable, dependent: :destroy
end
class Post < ActiveRecord::Base
belongs_to :district
has_one :department, through: :district
has_many :members, dependent: :destroy
has_many :officers, as: :fillable, dependent: :destroy
has_many :reports, dependent: :destroy
has_many :markups, as: :markupable, dependent: :destroy
has_many :users, as: :loginable, dependent: :destroy
end
class User < ActiveRecord::Base
belongs_to :loginable, polymorphic: true
end
class Officer < ActiveRecord::Base
belongs_to :fillable, polymorphic: true
end
class Report < ActiveRecord::Base
belongs_to :post
end
与大多数这样的实体一样,底部完成所有工作(提交报告等),上层只管理,总结和报告。
这是一个半封闭系统,您必须至少登录一个“访客”,您可以在其中查看,但不能触摸。每个级别都有官员(员工)和标记(新闻,帖子等)。用户是属于其中一个实体(部门,区域或邮政)的多态关系。
我的主要路线基本上是浅路由,除了我使用Pundit进行范围界定:
resources :departments do
resources :officers,only: [:new, :create]
resources :markups,only: [:new, :create]
member do
get :reports_summary
end
end
resources :districts do
resources :officers,only: [:new, :create]
resources :markups,only: [:new, :create]
member do
get :reports_summary
end
end
resources :posts do
resources :officers,only: [:new, :create]
resources :markups,only: [:new, :create]
resources :reports,only: [:new, :create]
resources :members,only: [:new, :create]
member do
get :reports_menu
get :reports_summary
end
end
resources :officers, only: [:index, :show, :edit, :update, :destroy]
resources :markups, only: [:index, :show, :edit, :update, :destroy]
resources :reports, only: [:index, :show, :edit, :update, :destroy]
resources :members, only: [:index, :show, :edit, :update, :destroy]
只看邮政官员,生成的路线是:
post_officers_path POST /posts/:post_id/officers(.:format) officers#create
new_post_officer_path GET /posts/:post_id/officers/new(.:format) officers#new
我的问题是让控制器测试工作而不需要用我的余生来解决它!我确实使用脚手架生成模型和测试。我开始认为控制器测试是重复浪费时间并专注于集成测试。我的主要问题是:new和:为像人员这样的多态关联创建测试。我的旧钝力测试工作正常(知道它不是最好的方法)并且路由所有工作。控制器new和create tests将失败,并显示ActionController::UrlGenerationError: No route matches {:action=>"/posts/980190962/officers/new", :controller=>"officers"}
或No route matches {:action=>"new", :controller=>"officers"}
甚至将新职位的路线硬连线:
def test_new_post
get "/posts/#{@resource.id}/officers/new"
assert_response :success
end
失败。
我主要是看控制器测试来做基本的CRUD和授权,但我认为在集成测试中更容易测试授权,特别是如果有不同的角色来做不同的东西 - 但我应该能够把这个想出来(解决;计算出;弄明白。我已经查看了stackoverflow上似乎数百个帖子的内容,但似乎都没有解决多态嵌套资源。