FIX: Flaky test

FIX: Flaky test

The previous solution was not always working, I believe this one will be consistent.

diff --git a/app/controllers/category_hashtags_controller.rb b/app/controllers/category_hashtags_controller.rb
index f063851..4f6601e 100644
--- a/app/controllers/category_hashtags_controller.rb
+++ b/app/controllers/category_hashtags_controller.rb
@@ -10,7 +10,7 @@ class CategoryHashtagsController < ApplicationController
 
     slugs_and_urls = {}
 
-    Category.secured(guardian).where(id: ids).each do |category|
+    Category.secured(guardian).where(id: ids).order(:id).each do |category|
       slugs_and_urls[category.slug] ||= category.url
       slugs_and_urls[category.slug_path.last(2).join(':')] ||= category.url
     end
diff --git a/spec/requests/category_hashtags_controller_spec.rb b/spec/requests/category_hashtags_controller_spec.rb
index 00f0233..f4135ae 100644
--- a/spec/requests/category_hashtags_controller_spec.rb
+++ b/spec/requests/category_hashtags_controller_spec.rb
@@ -66,10 +66,6 @@ describe CategoryHashtagsController do
           quxbar = Fabricate(:category_with_definition, slug: "bar", parent_category_id: qux.id)
           quxbarbaz = Fabricate(:category_with_definition, slug: "baz", parent_category_id: quxbar.id)
 
-          first_bar = foobar.id < quxbar.id ? foobar : quxbar
-          first_baz = foobarbaz.id < quxbarbaz.id ? foobarbaz : quxbarbaz
-          first_barbaz = foobar.id < quxbar.id ? foobarbaz : quxbarbaz
-
           get "/category_hashtags/check.json", params: {
             category_slugs: [
               ":",
@@ -88,10 +84,10 @@ describe CategoryHashtagsController do
           expect(response.status).to eq(200)
           expect(response.parsed_body["valid"]).to contain_exactly(
             { "slug" => "foo",     "url" => foo.url },
-            { "slug" => "bar",     "url" => first_bar.url },
+            { "slug" => "bar",     "url" => foobar.url },
             { "slug" => "foo:bar", "url" => foobar.url },
-            { "slug" => "baz",     "url" => first_baz.url },
-            { "slug" => "bar:baz", "url" => first_barbaz.url },
+            { "slug" => "baz",     "url" => foobarbaz.url },
+            { "slug" => "bar:baz", "url" => foobarbaz.url },
             { "slug" => "qux",     "url" => qux.url },
             { "slug" => "qux:bar", "url" => quxbar.url }
           )

GitHub sha: c2ce7f26

1 Like