Rspec Rails中没有路由匹配

时间:2015-04-27 19:18:05

标签: ruby ruby-on-rails-4 rspec-rails

我正在使用Rails 4.1,我有一个API json,但不适用于Rspec-Rails。

rspec-rails - 版本使用3.2.1

$ rake routes

GET  /cities/:code(.:format)     cities#show

cities_controller.rb

class CitiesController < ApplicationController
  respond_to :json
  def show
    code = params[:code]
    respond_with City.find_by_code(code)
  end
end

cities_controller_spec.rb

require 'rails_helper'

RSpec.describe CitiesController, :type => :controller do
  describe "GET city" do
    it "should be ok" do
      get '/cities', :code => 'DUB', :format => :json
      expect(response).to be_success
      expect(response).to have_http_status(200)
    end
  end
end

当我执行rspec时,我收到了这个

Failure/Error: data = get '/cities', :code => 'DUB', :format => :json
     ActionController::UrlGenerationError:
       No route matches {:action=>"/cities", :controller=>"cities", :code=>"DUB", :format=>:json}

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

请改用:

get :show, :code => 'DUB', :format => :json

get方法接受控制器操作的名称,而不是路径。有关详细信息,请参阅the documentation