Search code examples
ruby-on-railsrubyrspecruby-hash

RSpec routing spec: sequence of fields in hash


I'm having a problem with the actual vs expected failing due to sequence on the hash. I don't recall seeing this before ... and in any case I thought a hash was unordered?

How can I have this test pass?

RSpec.describe ArticleSectionsController, type: :routing do
  describe "routing" do

    it "routes to #index" do
      expect(:get => "/articles/5/article_sections").to route_to("article_sections#index", article_id: 5)
    end
  end
end
  1) ArticleSectionsController routing routes to #index
     Failure/Error: expect(:get => "/articles/5/article_sections").to route_to("article_sections#index", article_id: 5)

       The recognized options <{"controller"=>"article_sections", "action"=>"index", "article_id"=>"5"}> did not match <{"article_id"=>5, "controller"=>"article_sections", "action"=>"index"}>, difference:.
       --- expected
       +++ actual
       @@ -1 +1 @@
        -{"article_id"=>5, "controller"=>"article_sections", "action"=>"index"}
       +{"controller"=>"article_sections", "action"=>"index", "article_id"=>"5"}

Solution

  • The problem is not the order of the hash, the problem is the content.

    "article_id"=>"5"
    

    is not the same as

    "article_id"=> 5
    

    Us the string version in your route_to parameters and that'll fix the issue.