模块内控制器的规格(版本主义者) [英] Specs for controller inside a module (versionist)
问题描述
我正在 Rails 中创建 API,我使用 versionist 来处理版本.我想测试 API 控制器,但无法创建有效请求.
I'm creating an API in Rails and I use versionist to handle versions. I want to test API controllers, but I'm unable to create a valid request.
我的控制器:
class Api::V1::ItemsController < Api::V1::BaseController
def index
render json:'anything'
end
end
我的规格:
describe Api::V1::ItemsController do
describe "#create" do
it "shows items" do
get :index, format: :json
end
end
end
routes.rb:
scope '/api' do
api_version(:module => "Api::V1", :path => {:value => "v1"}, :default => true) do
resources :items
end
end
该测试不检查任何内容.尽管如此,它还是会引发错误:
The test doesn't check anything. Still, it raises an error:
Failure/Error: get :index, format: :json
ActionController::RoutingError:
No route matches {:format=>:json, :controller=>"api/v1/items", :action=>"index"}
我认为请求中的 :controller
键有问题,但我不知道如何修复它...
I suppose that there is something wrong with the :controller
key in the request, but I don't know how to fix it...
推荐答案
我能够在本地重现此问题.您需要将其移动到请求规范而不是控制器规范才能使其工作:
I was able to reproduce this locally. You need to move this to a request spec instead of a controller spec for this to work:
# spec/requests/api/v1/items_controller_spec.rb
describe Api::V1::ItemsController do
describe "#index" do
it "shows items" do
get '/api/v1/items.json'
# assert something
end
end
end
versionist 文档说在使用 HTTP 标头或请求参数版本控制策略时需要执行此操作 (https://github.com/bploetz/versionist#a-note-about-testing-when-using-the-http-header-or-request-parameter-strategies) 但这里显然不是这种情况.我将提交一个问题以在文档中澄清您需要为所有版本控制策略执行此操作.
The versionist documentation says you need to do this when using the HTTP header or request parameter versioning strategies (https://github.com/bploetz/versionist#a-note-about-testing-when-using-the-http-header-or-request-parameter-strategies) but that's clearly not the case here. I'll file an issue to get this clarified in the documentation that you need to do this for all versioning strategies.
这篇关于模块内控制器的规格(版本主义者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!