Specs for controller inside a module (versionist)

我与影子孤独终老i 提交于 2019-12-24 05:49:06

问题


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.

My controller:

 class Api::V1::ItemsController < Api::V1::BaseController    
  def index
    render json:'anything'
  end
end

My spec:

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"}

I suppose that there is something wrong with the :controller key in the request, but I don't know how to fix it...


回答1:


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

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.



来源:https://stackoverflow.com/questions/14586573/specs-for-controller-inside-a-module-versionist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!