Correct erratic spec failure

Correct erratic spec failure

diff --git a/app/models/category_featured_topic.rb b/app/models/category_featured_topic.rb
index 22d948d..a517064 100644
--- a/app/models/category_featured_topic.rb
+++ b/app/models/category_featured_topic.rb
@@ -23,7 +23,13 @@ class CategoryFeaturedTopic < ActiveRecord::Base
       no_definitions: true
     }
 
-    # Add topics, even if they're in secured categories:
+    # It may seem a bit odd that we are running 2 queries here, when admin
+    # can clearly pull out all the topics needed.
+    # We do so, so anonymous will ALWAYS get some topics
+    # If we only fetched as admin we may have a situation where anon can see
+    # no featured topics (all the previous 2x topics are only visible to admins)
+
+    # Add topics, even if they're in secured categories or invisible
     query = TopicQuery.new(CategoryFeaturedTopic.fake_admin, query_opts)
     results = query.list_category_topic_ids(c).uniq
 
diff --git a/spec/models/category_featured_topic_spec.rb b/spec/models/category_featured_topic_spec.rb
index ee10237..7c9a611 100644
--- a/spec/models/category_featured_topic_spec.rb
+++ b/spec/models/category_featured_topic_spec.rb
@@ -34,7 +34,7 @@ describe CategoryFeaturedTopic do
 
     it 'should feature stuff in the correct order' do
       category = Fabricate(:category, num_featured_topics: 2)
-      t5 = Fabricate(:topic, category_id: category.id, bumped_at: 12.minutes.ago)
+      _t5 = Fabricate(:topic, category_id: category.id, bumped_at: 12.minutes.ago)
       t4 = Fabricate(:topic, category_id: category.id, bumped_at: 10.minutes.ago)
       t3 = Fabricate(:topic, category_id: category.id, bumped_at: 7.minutes.ago)
       t2 = Fabricate(:topic, category_id: category.id, bumped_at: 4.minutes.ago)
@@ -45,7 +45,7 @@ describe CategoryFeaturedTopic do
 
       # Should find more than we need: pinned topics first, then num_featured_topics * 2
       expect(
-        CategoryFeaturedTopic.where(category_id: category.id).pluck(:topic_id)
+        CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)
       ).to eq([pinned.id, t2.id, t1.id, t3.id, t4.id])
 
     end

GitHub sha: c2a66160

This is a great little example of the follies of relying on database ordering.

1 Like