REFACTOR: New spec tests and code improvement

This commit is contained in:
Vinoth Kannan
2018-02-22 20:27:02 +05:30
parent 84867c1c07
commit 7cbda949f1
12 changed files with 144 additions and 51 deletions

View File

@ -10,14 +10,15 @@ describe Tag do
end
end
let(:tag) { Fabricate(:tag) }
let(:topic) { Fabricate(:topic, tags: [tag]) }
before do
SiteSetting.tagging_enabled = true
SiteSetting.min_trust_level_to_tag_topics = 0
end
it "can delete tags on deleted topics" do
tag = Fabricate(:tag)
topic = Fabricate(:topic, tags: [tag])
topic.trash!
expect { tag.destroy }.to change { Tag.count }.by(-1)
end
@ -94,4 +95,14 @@ describe Tag do
end
end
end
context "topic counts" do
it "should exclude private message topics" do
topic
Fabricate(:private_message_topic, tags: [tag])
described_class.ensure_consistency!
tag.reload
expect(tag.topic_count).to eq(1)
end
end
end

View File

@ -0,0 +1,47 @@
require 'rails_helper'
describe TopicTag do
let(:topic) { Fabricate(:topic) }
let(:tag) { Fabricate(:tag) }
let(:topic_tag) { Fabricate(:topic_tag, topic: topic, tag: tag) }
context '#after_create' do
it "tag topic_count should be increased" do
expect {
topic_tag
}.to change(tag, :topic_count).by(1)
end
it "tag topic_count should not be increased" do
topic.archetype = Archetype.private_message
expect {
topic_tag
}.to change(tag, :topic_count).by(0)
end
end
context '#after_destroy' do
it "tag topic_count should be decreased" do
topic_tag
expect {
topic_tag.destroy
}.to change(tag, :topic_count).by(-1)
end
it "tag topic_count should not be decreased" do
topic.archetype = Archetype.private_message
topic_tag
expect {
topic_tag.destroy
}.to change(tag, :topic_count).by(0)
end
end
end