Question

My routing specs for rspec give unclear error back. In expected params v/s the real param the id param is at reversed position. Why and how to resolve?

require "spec_helper"

describe GameController do
  describe "routing" do

    game = FactoryGirl.create(:game)

    it "routes to #show" do
      get("/game/1").should route_to("game#show", :id => 1)
    end

  end
end

This throws error:

  1) gameController routing routes to #show
     Failure/Error: get("/game/1").should route_to("game#show", :id => 1)
       The recognized options <{"action"=>"show", "controller"=>"game", "id"=>"1"}> did not match <{"id"=>1, "controller"=>"game", "action"=>"show"}>, difference:.
       <{"id"=>1, "controller"=>"game", "action"=>"show"}> expected but was
       <{"action"=>"show", "controller"=>"game", "id"=>"1"}>.
     # ./spec/routing/game_routing_spec.rb:11:in `block (3 levels) in <top (required)>'
Was it helpful?

Solution

Rails parses the parameters as strings, not integers, so params[:id] is really being assigned "1" instead of 1.

Try expecting a string instead:

get("/game/1").should route_to("game#show", :id => "1")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top