我正在使用Rails创建API,我使用versionist来处理版本。我想测试API控制器,但我无法创建有效的请求。
我的控制器:
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
测试不检查任何内容。不过,它引发了一个错误:
Failure/Error: get :index, format: :json
ActionController::RoutingError:
No route matches {:format=>:json, :controller=>"api/v1/items", :action=>"index"}
我认为请求中的:controller
键有问题,但我不知道如何解决它...
答案 0 :(得分:2)
我能够在本地重现这个。您需要将其移动到请求规范而不是控制器规范才能使其工作:
# 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
版本主义文档说您在使用HTTP标头或请求参数版本控制策略(https://github.com/bploetz/versionist#a-note-about-testing-when-using-the-http-header-or-request-parameter-strategies)时需要执行此操作,但这显然不是这种情况。我将提交一个问题,以便在所有版本控制策略所需的文档中明确说明。