I start unit test implementation in my REST Api application.
I use versionist gem.
So, I have a class named like this : Api::V0_1_0::TestController
class Api::V0_1_0::TestController < Api::V0_1_0::BaseController
def test
respond_with({
:message => "done"
})
end
end
routes.rb (without all routes, but just the necessary):
MyApp::Application.routes.draw do
# -----------------------------------------------------------------------------------------------------------------------------------------
# ApiVersions
#
scope :module => "api",
:defaults => {:format => "json"} do
# ---------------------------------------------------------------------------------------------------
# V0.1.0
# Default version
#
api_version :module => "V0__1__0",
:header => "X-Version", :value => "0.1.0" do
# Test controller
get "/test" => "test#test"
end
# ---------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------
# V0.1.1
# Default version
#
api_version :module => "V0__1__1",
:header => "X-Version", :value => "0.1.1" do
# Test Controller
get "/test" => "test#test"
end
# -----------------------------------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------------------------------
# ActiveAdmin routes
#
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
# -----------------------------------------------------------------------------------------------------------------------------------------
end
Functional test file :
class Api::V0_1_0::TestControllerTest < ActionController::TestCase
test "should get test" do
get :test
assert_response :success
end
end
When I launch rake test:functionals.
I have this error :
No route matches {:controller=>”api/v0_1_0/test”, :action=>”test”}
And when I launch rake routes, I have :
test GET /test(.:format) {:format=>"json", :controller=>"api/V0__1__0/test", :action=>"test"}
With double underscore for V0__1__0 and not V0_1_0.
I don’t know how resolve this problem.
EDIT: I have add routes.rb.
NOTE: I’ve also post an issue here : https://github.com/bploetz/versionist/issues/9
Rails functional tests (ActionController::TestCase) are for testing controller action methods in isolation. They do not go through the full Rails stack, specifically the Rails dispatcher code path, which is where versionist hooks in to do it’s thing.
If you want to test your versioned API routes which rely on the HTTP header strategy, use integration tests (ActionDispatch::IntegrationTest) or request specs if you’re using rspec.
Versionist 0.2.0 added integration tests/request specs to the components created by it’s generators (https://github.com/bploetz/versionist/issues/11).