1

我在我的 REST Api 应用程序中启动单元测试实现。我使用版本主义 gem

所以,我有一个这样的类: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(没有所有路线,但只是必要的):

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

功能测试文件:

class Api::V0_1_0::TestControllerTest < ActionController::TestCase

  test "should get test" do
    get :test
    assert_response :success
  end
end

当我启动rake test:functionals. 我有这个错误:

没有路由匹配 {:controller=>"api/v0_1_0/test", :action=>"test"}

当我启动时rake routes,我有:

test GET  /test(.:format)   {:format=>"json", :controller=>"api/V0__1__0/test", :action=>"test"}

带有双下划线 forV0__1__0和 not V0_1_0

我不知道如何解决这个问题。

编辑:我添加了routes.rb.

注意:我在这里也发布了一个问题:https ://github.com/bploetz/versionist/issues/9

4

2 回答 2

0

看看你提供的链接,你能做这样的事情吗?

api_version :module => "V0__1__0",
            :path => "/V0_1_0",
            :header => "X-Version", :value => "0.1.0" do

或者

get "/test" => "test#test", :controller => "api/V0_1_0/test"
于 2012-04-24T13:41:05.733 回答
0

Rails 功能测试(ActionController::TestCase)用于单独测试控制器操作方法。他们没有通过完整的 Rails 堆栈,特别是 Rails 调度程序代码路径,这是版本主义挂钩的地方。

如果您想测试依赖于 HTTP 标头策略的版本化 API 路由,请使用集成测试 (ActionDispatch::IntegrationTest) 或在使用 rspec 时请求规范。

require 'test_helper'

class Api::V0_1_0::TestControllerTest < ActionDispatch::IntegrationTest

  test "should get test" do
    get '/test', nil, {"X-Version" => "0.1.0"}
    assert_response :success
    assert_equal @response.body, "0.1.0"
  end
end

Versionist 0.2.0 向其生成器创建的组件添加了集成测试/请求规范 ( https://github.com/bploetz/versionist/issues/11 )。

于 2012-04-27T22:44:59.803 回答