Alexander
Alexander

Reputation: 3

No route matches in Rspec Rails

I'm using Rails 4.1 and I have an API json, but doesn't work on Rspec-Rails.

rspec-rails - Version used 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

When I execute rspec, I receive this

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

Any ideas?

Thanks!!

Upvotes: 0

Views: 1504

Answers (1)

fivedigit
fivedigit

Reputation: 18682

Use this instead:

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

The get method accepts the name of the controller action, not a path. See the documentation for more information.

Upvotes: 2

Related Questions