不确定如何正确测试

时间:2014-05-07 14:57:00

标签: ruby-on-rails rspec routes

我有一条匹配的路线:

get 'beat' => 'heartbeat#beat', as: => 'beat'
#=> api_v1_beat GET  /api/v1/beat(.:format)   api/v1/heartbeat#beat

然后我有一个特别基本的控制器!

module Api
  module V1
    class HeartBeatController < BaseController

      def beat
        respond json: {}, status: 200
      end

    end
  end
end

概念是你每隔15-30秒就会ping一次,看看是否活着。

现在我需要对此进行测试,

require 'spec_helper'

describe Api::V1::HeartBeatController do
  context "200" do
    it "should ping the  heartbeat and return 200" do
      get :beat
      expect(response.code).to eql '200'
    end
  end
end

但它失败了:

 Failure/Error: get :beat
 ActionController::UrlGenerationError:
   No route matches {:action=>"beat", :controller=>"api/v1/heart_beat"}
呃???它的陈述非常明显,但也许我错过了一些如此简陋和基本的东西?

2 个答案:

答案 0 :(得分:1)

尝试:

get 'beat' => 'heart_beat#beat', as: => 'beat'

其他人注意事项: 在Rails 4中,路径中控制器对象的名称必须是snake_case,用于多个字控制器。

Ex:
get 'action' => 'snake_case_controller_name#action'

答案 1 :(得分:0)

也写一个路由测试:

describe HeartBeatController do
  describe "routing" do
    it "routes to #beat" do
      get("/beat}").should route_to("api/v1/heart_beat#beat")
    end
  end
end