Fixes for blog_post specs

* spec "returns all posts from specified month" failed on the second day of a month
* refactorings and cleanups (while I'm at it)
This commit is contained in:
Marc Remolt 2011-05-02 19:27:21 +02:00 committed by Philip Arndt
parent 76708704c8
commit fa9e6dae6d

View file

@ -2,9 +2,7 @@ require 'spec_helper'
Dir[File.expand_path('../../../features/support/factories/*.rb', __FILE__)].each{|factory| require factory} Dir[File.expand_path('../../../features/support/factories/*.rb', __FILE__)].each{|factory| require factory}
describe BlogPost do describe BlogPost do
before(:each) do let(:blog_post ) { Factory :post }
@blog_post = Factory(:post)
end
describe "validations" do describe "validations" do
it "requires title" do it "requires title" do
@ -12,7 +10,7 @@ describe BlogPost do
end end
it "won't allow duplicate titles" do it "won't allow duplicate titles" do
Factory.build(:post, :title => @blog_post.title).should_not be_valid Factory.build(:post, :title => blog_post.title).should_not be_valid
end end
it "requires body" do it "requires body" do
@ -23,28 +21,28 @@ describe BlogPost do
describe "comments association" do describe "comments association" do
it "have a comments attribute" do it "have a comments attribute" do
@blog_post.should respond_to(:comments) blog_post.should respond_to(:comments)
end end
it "destroys associated comments" do it "destroys associated comments" do
Factory(:blog_comment, :blog_post_id => @blog_post.id) Factory(:blog_comment, :blog_post_id => blog_post.id)
@blog_post.destroy blog_post.destroy
BlogComment.find_by_blog_post_id(@blog_post.id).should == nil BlogComment.find_by_blog_post_id(blog_post.id).should == nil
end end
end end
describe "categories association" do describe "categories association" do
it "have categories attribute" do it "have categories attribute" do
@blog_post.should respond_to(:categories) blog_post.should respond_to(:categories)
end end
end end
describe "tags" do describe "tags" do
it "acts as taggable" do it "acts as taggable" do
@blog_post.should respond_to(:tag_list) blog_post.should respond_to(:tag_list)
#the factory has default tags, including 'chicago' #the factory has default tags, including 'chicago'
@blog_post.tag_list.should include("chicago") blog_post.tag_list.should include("chicago")
end end
end end
@ -55,57 +53,60 @@ describe BlogPost do
end end
describe "by_archive scope" do describe "by_archive scope" do
before do
@blog_post1 = Factory(:post, :published_at => Date.new(2011, 3, 11))
@blog_post2 = Factory(:post, :published_at => Date.new(2011, 3, 12))
#2 months before
Factory(:post, :published_at => Date.new(2011, 1, 10))
end
it "returns all posts from specified month" do it "returns all posts from specified month" do
BlogPost.delete_all
#this month
blog_post1 = Factory(:post, :published_at => Time.now - 2.days)
blog_post2 = Factory(:post, :published_at => Time.now - 1.day)
#2 months ago
Factory(:post, :published_at => Time.now - 2.months)
#check for this month #check for this month
date = "#{Time.now.month}/#{Time.now.year}" date = "03/2011"
BlogPost.by_archive(Time.parse(date)).count.should == 2 BlogPost.by_archive(Time.parse(date)).count.should == 2
BlogPost.by_archive(Time.parse(date)).should == [blog_post2, blog_post1] BlogPost.by_archive(Time.parse(date)).should == [@blog_post2, @blog_post1]
end end
end end
describe "all_previous scope" do describe "all_previous scope" do
it "returns all posts from previous months" do before do
BlogPost.delete_all @blog_post1 = Factory(:post, :published_at => Time.now - 2.months)
@blog_post2 = Factory(:post, :published_at => Time.now - 1.month)
Factory :post, :published_at => Time.now
end
blog_post1 = Factory(:post, :published_at => Time.now - 2.months) it "returns all posts from previous months" do
blog_post2 = Factory(:post, :published_at => Time.now - 1.month)
Factory(:post, :published_at => Time.now)
BlogPost.all_previous.count.should == 2 BlogPost.all_previous.count.should == 2
BlogPost.all_previous.should == [blog_post2, blog_post1] BlogPost.all_previous.should == [@blog_post2, @blog_post1]
end end
end end
describe "live scope" do describe "live scope" do
it "returns all posts which aren't in draft and pub date isn't in future" do before do
BlogPost.delete_all @blog_post1 = Factory(:post, :published_at => Time.now.advance(:minutes => -2))
@blog_post2 = Factory(:post, :published_at => Time.now.advance(:minutes => -1))
blog_post1 = Factory(:post, :published_at => Time.now.advance(:minutes => -2))
blog_post2 = Factory(:post, :published_at => Time.now.advance(:minutes => -1))
Factory(:post, :draft => true) Factory(:post, :draft => true)
Factory(:post, :published_at => Time.now + 1.minute) Factory(:post, :published_at => Time.now + 1.minute)
end
it "returns all posts which aren't in draft and pub date isn't in future" do
BlogPost.live.count.should == 2 BlogPost.live.count.should == 2
BlogPost.live.should == [blog_post2, blog_post1] BlogPost.live.should == [@blog_post2, @blog_post1]
end end
end end
describe "uncategorized scope" do describe "uncategorized scope" do
before do
@uncategorized_blog_post = Factory(:post)
@categorized_blog_post = Factory(:post)
@categorized_blog_post.categories << Factory(:blog_category)
end
it "returns uncategorized posts if they exist" do it "returns uncategorized posts if they exist" do
uncategorized_blog_post = Factory(:post) BlogPost.uncategorized.should include @uncategorized_blog_post
categorized_blog_post = Factory(:post) BlogPost.uncategorized.should_not include @categorized_blog_post
categorized_blog_post.categories << Factory(:blog_category)
BlogPost.uncategorized.should include uncategorized_blog_post
BlogPost.uncategorized.should_not include categorized_blog_post
end end
end end
@ -124,52 +125,63 @@ describe BlogPost do
end end
describe "#next" do describe "#next" do
it "returns next article when called on current article" do before do
BlogPost.delete_all
Factory(:post, :published_at => Time.now.advance(:minutes => -1)) Factory(:post, :published_at => Time.now.advance(:minutes => -1))
blog_post = Factory(:post) @blog_post = Factory(:post)
blog_posts = BlogPost.all end
blog_posts.last.next.should == blog_post
it "returns next article when called on current article" do
BlogPost.last.next.should == @blog_post
end end
end end
describe "#prev" do describe "#prev" do
it "returns previous article when called on current article" do before do
BlogPost.delete_all
Factory(:post) Factory(:post)
blog_post = Factory(:post, :published_at => Time.now.advance(:minutes => -1)) @blog_post = Factory(:post, :published_at => Time.now.advance(:minutes => -1))
blog_posts = BlogPost.all end
blog_posts.first.prev.should == blog_post
it "returns previous article when called on current article" do
BlogPost.first.prev.should == @blog_post
end end
end end
describe "#category_ids=" do describe "#category_ids=" do
before(:each) do before do
@cat1 = Factory(:blog_category, :id => 1) @cat1 = Factory(:blog_category, :id => 1)
@cat2 = Factory(:blog_category, :id => 2) @cat2 = Factory(:blog_category, :id => 2)
@cat3 = Factory(:blog_category, :id => 3) @cat3 = Factory(:blog_category, :id => 3)
@blog_post.category_ids = [1,2,"","",3] blog_post.category_ids = [1,2,"","",3]
end end
it "rejects blank category ids" do it "rejects blank category ids" do
@blog_post.categories.count.should == 3 blog_post.categories.count.should == 3
end end
it "returns array of categories based on given ids" do it "returns array of categories based on given ids" do
@blog_post.categories.should == [@cat1, @cat2, @cat3] blog_post.categories.should == [@cat1, @cat2, @cat3]
end end
end end
describe ".comments_allowed?" do describe ".comments_allowed?" do
it "returns true if comments_allowed setting is set to true" do context "with RefinerySetting comments_allowed set to true" do
BlogPost.comments_allowed?.should be_true before do
RefinerySetting.set(:comments_allowed, { :scoping => 'blog', :value => true })
end end
it "returns false if comments_allowed setting is set to false" do it "should be true" do
BlogPost.comments_allowed?.should be_true
end
end
context "with RefinerySetting comments_allowed set to true" do
before do
RefinerySetting.set(:comments_allowed, { :scoping => 'blog', :value => false }) RefinerySetting.set(:comments_allowed, { :scoping => 'blog', :value => false })
end
it "should be false" do
BlogPost.comments_allowed?.should be_false BlogPost.comments_allowed?.should be_false
end end
end end
end end
end