-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor model test for article and user
- Loading branch information
Showing
6 changed files
with
70 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,5 @@ group :development, :test do | |
gem 'pry-rails' | ||
|
||
gem 'rspec-rails' | ||
gem "shoulda-matchers" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
zh_CN: | ||
zh-CN: | ||
home: 首页 | ||
about: 关于 | ||
acknowledge: 感谢 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,119 @@ | ||
require 'spec_helper' | ||
|
||
describe Article do | ||
before(:each) do | ||
@article = create :article | ||
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 | ||
|
||
context 'validation' do | ||
it 'is valide' do | ||
expect(@article).to be_valid | ||
end | ||
|
||
it 'is invalid when title is empty' do | ||
@article.title = '' | ||
expect(@article).to be_invalid | ||
end | ||
|
||
it 'is invalid when content is empty' do | ||
@article.content = '' | ||
expect(@article).to be_invalid | ||
end | ||
|
||
it 'is invalid when title have been used' do | ||
article = build :article, title: @article.title | ||
expect(article).to be_invalid | ||
end | ||
|
||
it 'is invalid when content length less than 20' do | ||
@article.content = 'a' * 19 | ||
expect(@article).to be_invalid | ||
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 'created two tags' do | ||
it "Tag's count is 2" do | ||
expect(Tag.count).to eq 2 | ||
tags = Tag.pluck :name | ||
expect(tags).to include 'tag1' | ||
expect(tags).to include 'tag2' | ||
end | ||
|
||
it "tag1's articles_count should be 1" do | ||
tag1 = Tag.find_by_name 'tag1' | ||
expect(tag1.articles_count).to eq 1 | ||
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 should be 1" do | ||
tag2 = Tag.find_by_name 'tag2' | ||
expect(tag2.articles_count).to eq 1 | ||
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.tag_list = "tag1" | ||
@article.save | ||
article.update_attribute :tag_list, "tag1" | ||
end | ||
|
||
it "tags' count is 2" do | ||
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 | ||
tag1 = Tag.find_by_name "tag1" | ||
expect(tag1.articles_count).to eq 1 | ||
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 | ||
tag2 = Tag.find_by_name "tag2" | ||
expect(tag2.articles_count).to be 0 | ||
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.tag_list = "tag2,tag3" | ||
@article.save | ||
article.update_attribute :tag_list, "tag2,tag3" | ||
end | ||
|
||
it "tag's count is 3" do | ||
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 | ||
tag1 = Tag.find_by_name "tag1" | ||
expect(tag1.articles_count).to eq 0 | ||
tag = Tag.find_by_name "tag1" | ||
expect(tag.articles_count).to eq 0 | ||
end | ||
|
||
it "tag2's articles_count is 1" do | ||
tag2 = Tag.find_by_name "tag2" | ||
expect(tag2.articles_count).to eq 1 | ||
tag = article.tags.find_by_name "tag2" | ||
expect(tag.articles_count).to eq 1 | ||
end | ||
|
||
it "tag3's articles_count is 1" do | ||
tag3 = Tag.find_by_name "tag3" | ||
expect(tag3.articles_count).to eq 1 | ||
tag = article.tags.find_by_name "tag3" | ||
expect(tag.articles_count).to eq 1 | ||
end | ||
end | ||
end | ||
|
||
describe '#draft?' do | ||
it "return false when tags exclude 'draft'" do | ||
expect(@article).not_to be_draft | ||
end | ||
|
||
context "change tag_list value to 'tag1,draft'" do | ||
before(:each) do | ||
@article.tag_list = "tag1,draft" | ||
@article.save | ||
end | ||
context "tag_list without 'draft'" do | ||
subject { create :article } | ||
|
||
it "Tag's count is 3" do | ||
expect(Tag.count).to eq 3 | ||
end | ||
it { should_not be_draft } | ||
end | ||
|
||
it "tag1's articles_count is 0" do | ||
tag1 = Tag.find_by_name "tag1" | ||
expect(tag1.articles_count).to eq 0 | ||
end | ||
context "tag_list value with 'draft'" do | ||
subject(:article) { create :article, tag_list: 'tag1,draft' } | ||
|
||
it "tag2's articles_count is 0" do | ||
tag2 = Tag.find_by_name "tag2" | ||
expect(tag2.articles_count).to eq 0 | ||
it "Tag's count is 1" do | ||
expect(article.tags.count).to eq 1 | ||
end | ||
|
||
it "draft's articles_count is 1" do | ||
draft = Tag.find_by_name "draft" | ||
expect(draft.articles_count).to eq 1 | ||
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 "return true when tags include 'draft'" do | ||
expect(@article).to be_draft | ||
end | ||
it { should be_draft } | ||
end | ||
|
||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
require 'spec_helper' | ||
|
||
describe User do | ||
subject(:user) { User.new APP_CONFIG[:admin_email] } | ||
|
||
it { should be_admin } | ||
|
||
it 'is not admin when email is not eqaul to admin_email' do | ||
user = User.new "non_admin@gmail.com" | ||
user.email = "non_admin@gmail.com" | ||
expect(user).not_to be_admin | ||
end | ||
|
||
it 'is admin when email is eqaul to admin_email' do | ||
user = User.new APP_CONFIG[:admin_email] | ||
expect(user).to be_admin | ||
end | ||
end |