RSpec 路由规范:哈希中的字段序列

RSpec routing spec: sequence of fields in hash

由于散列上的序列,我遇到了实际失败与预期失败的问题。我不记得以前见过这个……无论如何我认为散列是无序的?

我怎样才能通过这个测试?

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"}

问题不在于散列的顺序,问题在于内容。

"article_id"=>"5"

不一样
"article_id"=> 5

我们在您的 route_to 参数中使用字符串版本,这将解决问题。