discourse/spec/models/category_featured_topic_spec.rb
Andy Waite 3e50313fdc Prepare for separation of RSpec helper files
Since rspec-rails 3, the default installation creates two helper files:
* `spec_helper.rb`
* `rails_helper.rb`

`spec_helper.rb` is intended as a way of running specs that do not
require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's
current `spec_helper.rb` does).

For more information:

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

In this commit, I've simply replaced all instances of `spec_helper` with
`rails_helper`, and renamed the original `spec_helper.rb`.

This brings the Discourse project closer to the standard usage of RSpec
in a Rails app.

At present, every spec relies on loading Rails, but there are likely
many that don't need to. In a future pull request, I hope to introduce a
separate, minimal `spec_helper.rb` which can be used in tests which
don't rely on Rails.
2015-12-01 20:39:42 +00:00

59 lines
2.2 KiB
Ruby

require 'rails_helper'
describe CategoryFeaturedTopic do
it { is_expected.to belong_to :category }
it { is_expected.to belong_to :topic }
context 'feature_topics_for' do
let(:user) { Fabricate(:user) }
let(:category) { Fabricate(:category) }
let!(:category_post) { PostCreator.create(user, raw: "I put this post in the category", title: "categorize THIS", category: category.id) }
it "should feature topics for a secure category" do
# so much dancing, I am thinking fixures make sense here.
user.change_trust_level!(TrustLevel[1])
category.set_permissions(:trust_level_1 => :full)
category.save
_uncategorized_post = PostCreator.create(user, raw: "this is my new post 123 post", title: "hello world")
CategoryFeaturedTopic.feature_topics_for(category)
expect(CategoryFeaturedTopic.count).to be(1)
end
it 'should not include invisible topics' do
invisible_post = PostCreator.create(user, raw: "Don't look at this post because it's awful.", title: "not visible to anyone", category: category.id)
invisible_post.topic.update_status('visible', false, Fabricate(:admin))
CategoryFeaturedTopic.feature_topics_for(category)
expect(CategoryFeaturedTopic.count).to be(1)
end
it 'should feature stuff in the correct order' do
SiteSetting.stubs(:category_featured_topics).returns(2)
category = Fabricate(:category)
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)
t1 = Fabricate(:topic, category_id: category.id, bumped_at: 5.minutes.ago)
pinned = Fabricate(:topic, category_id: category.id, pinned_at: 10.minutes.ago, bumped_at: 10.minutes.ago)
CategoryFeaturedTopic.feature_topics_for(category)
# Should find more than we need: pinned topics first, then category_featured_topics * 2
expect(
CategoryFeaturedTopic.where(category_id: category.id).pluck(:topic_id)
).to eq([pinned.id, t2.id, t1.id, t3.id, t4.id])
end
end
end