Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec routing specs give failure with param id reversed?

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)>'
like image 285
Rubytastic Avatar asked Jan 13 '23 04:01

Rubytastic


1 Answers

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")
like image 116
Dylan Markow Avatar answered Jan 18 '23 18:01

Dylan Markow