Skip to content

Commit

Permalink
Refactor model test for article and user
Browse files Browse the repository at this point in the history
  • Loading branch information
reyesyang committed Dec 19, 2013
1 parent 5b54028 commit c19a187
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 87 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -60,4 +60,5 @@ group :development, :test do
gem 'pry-rails'

gem 'rspec-rails'
gem "shoulda-matchers"
end
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -132,6 +132,8 @@ GEM
railties (>= 4.0.0.beta, < 5.0)
sass (>= 3.1.10)
sprockets-rails (~> 2.0.0)
shoulda-matchers (2.4.0)
activesupport (>= 3.0.0)
slop (3.4.6)
sprockets (2.10.0)
hike (~> 1.2)
Expand Down Expand Up @@ -188,6 +190,7 @@ DEPENDENCIES
rspec-rails
rvm-capistrano
sass-rails (~> 4.0.0)
shoulda-matchers
therubyracer
turbolinks
twitter-bootstrap-rails
Expand Down
2 changes: 1 addition & 1 deletion app/models/article.rb
Expand Up @@ -10,7 +10,7 @@ class Article < ActiveRecord::Base
self.per_page = 10

def tag_list=(value)
tags = value.split(',').map{ |tag| tag.strip.downcase }.reject{ |t| t.blank? }
tags = value.split(',').map { |tag| tag.strip.downcase }.reject { |t| t.blank? }

self.tags = if tags.include?('draft')
[Tag.find_or_create_by(name: 'draft')]
Expand Down
2 changes: 1 addition & 1 deletion config/locales/zh_CN.yml → config/locales/zh-CN.yml
@@ -1,4 +1,4 @@
zh_CN:
zh-CN:
home: 首页
about: 关于
acknowledge: 感谢
Expand Down
138 changes: 59 additions & 79 deletions spec/models/article_spec.rb
@@ -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
11 changes: 5 additions & 6 deletions spec/models/user_spec.rb
@@ -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

0 comments on commit c19a187

Please sign in to comment.