Ruby on rails rspec中get:index和get admin_categories_路径之间的差异
我正在用rspec测试我的控制器,遇到了一个有趣的问题。 我有我的控制器,Ruby on rails rspec中get:index和get admin_categories_路径之间的差异,ruby-on-rails,rspec,routes,constraints,Ruby On Rails,Rspec,Routes,Constraints,我正在用rspec测试我的控制器,遇到了一个有趣的问题。 我有我的控制器,Admin::CategoriesController,带有索引操作。我对路线有限制: resources :categories, only: [:edit, :update, :destroy] do collection do get ':kind', action: :index, as: '', kind: /(blog|news)/ end end 所以,当我在索引上编写规范时: it 'do
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)
我得到了绿色的结果。那么区别是什么呢?您编写了什么样的测试(功能/请求还是功能测试) 对于函数(控制器文件夹),您在控制器的范围内,并使用
get:index
在功能或请求测试的情况下,您不在控制器的范围内,您应该使用完整路径
user\u path
我正在编写控制器测试。但正如我在问题中所说:索引只是给出了不正确的结果