我用rspec测试我的控制器并遇到了一个有趣的问题。
我的控制器Admin::CategoriesController
带有索引操作。我对路线有限制:
resources :categories, only: [:edit, :update, :destroy] do
collection do
get ':kind', action: :index, as: '', kind: /(blog|news)/
end
end
所以,当我在索引上编写规范时:
it 'does not reply on not existing kind' do
expect { get :index, kind: 'qwe' }.to raise_error(ActionController::RoutingError)
end
它过去了。但后来我决定采取路线的约束:
class CategoryKindConstraint
def self.matches?(request)
%w(blog news).include? request.query_parameters['kind']
end
end
get ':kind', action: :index, as: '', constraints: CategoryKindConstraint
并且测试开始失败。没有例外。 但是再次 - 如果我改为:
expect { get admin_categories_path(kind: 'qwe') }.to raise_error(ActionController::RoutingError)
我得到了绿色结果。那有什么区别?
答案 0 :(得分:0)
您撰写了哪些类型的测试(功能/请求或功能)?
如果是功能(控制器文件夹),则您在控制器的范围内,并使用get :index
。
如果是功能或请求测试,您不在控制器的范围内,您应该使用完整路径user_path
。