/
article_spec.rb
119 lines (93 loc) · 2.87 KB
/
article_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require 'spec_helper'
describe Article do
describe 'validations' do
it { should validate_presence_of :title }
it { should validate_presence_of :content }
it { should validate_uniqueness_of :title }
it { should ensure_length_of(:content).is_at_least(20) }
end
describe "assosiations" do
it { should have_many :taggings }
it { should have_many :tags }
end
describe '#tag_list' do
subject!(:article) { create :article }
context "assign to default value 'tag1,tag2'" do
it "Tag's count is 2" do
expect(Tag.count).to eq 2
end
its("tags.count") { should eq 2 }
it "tag1's articles_count is 1" do
tag = article.tags.find_by_name 'tag1'
expect(tag.articles_count).to eq 1
end
it "tag2's articles_count is 1" do
tag = article.tags.find_by_name 'tag2'
expect(tag.articles_count).to eq 1
end
end
context "change value to 'tag1'" do
before(:each) do
article.update_attribute :tag_list, "tag1"
end
it "Tag's count is 2" do
expect(Tag.count).to eq 2
end
its("tags.count") { should eq 1 }
it "tag1's articles_count is 1" do
tag = article.tags.find_by_name "tag1"
expect(tag.articles_count).to eq 1
end
it "tagging for tag2 have been deleted" do
tag = article.tags.find_by_name "tag2"
expect(tag).to be_nil
end
it "tag2's articles_count is 0" do
tag = Tag.find_by_name "tag2"
expect(tag.articles_count).to be 0
end
end
context "change value to 'tag2,tag3'" do
before(:each) do
article.update_attribute :tag_list, "tag2,tag3"
end
it "Tag's count is 3" do
expect(Tag.count).to eq 3
end
its("tags.count") { should eq 2 }
it "tagging for tag1 have been deleted" do
tag = article.tags.find_by_name "tag1"
expect(tag).to be_nil
end
it "tag1's articles_count is 0 " do
tag = Tag.find_by_name "tag1"
expect(tag.articles_count).to eq 0
end
it "tag2's articles_count is 1" do
tag = article.tags.find_by_name "tag2"
expect(tag.articles_count).to eq 1
end
it "tag3's articles_count is 1" do
tag = article.tags.find_by_name "tag3"
expect(tag.articles_count).to eq 1
end
end
end
describe '#draft?' do
context "tag_list without 'draft'" do
subject { create :article }
it { should_not be_draft }
end
context "tag_list value with 'draft'" do
subject(:article) { create :article, tag_list: 'tag1,draft' }
it "Tag's count is 1" do
expect(article.tags.count).to eq 1
end
it "draft tag's articles_count is 1" do
tag = article.tags.find_by_name "draft"
expect(tag.articles_count).to eq 1
end
it { should be_draft }
end
end
end