aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUģis Ozols <ugis.ozolss@gmail.com>2010-12-07 09:09:55 +0200
committerUģis Ozols <ugis.ozolss@gmail.com>2010-12-07 09:09:55 +0200
commit9ff63388f31b20f1ce76b9bc61147720b801dfae (patch)
tree43333f8385e24655f466fa49063920ce0324bea7
parentffa7b65e82038afd499459afd4c3fd9ad0538e9f (diff)
downloadrefinerycms-blog-9ff63388f31b20f1ce76b9bc61147720b801dfae.tar.gz
refinerycms-blog-9ff63388f31b20f1ce76b9bc61147720b801dfae.tar.bz2
refinerycms-blog-9ff63388f31b20f1ce76b9bc61147720b801dfae.zip
Added some rspec tests for blog post model.
-rw-r--r--app/models/blog_post.rb1
-rw-r--r--features/support/factories/blog_categories.rb2
-rw-r--r--features/support/factories/blog_posts.rb4
-rw-r--r--spec/models/blog_posts_spec.rb157
4 files changed, 155 insertions, 9 deletions
diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb
index ccd10e2..1fb9900 100644
--- a/app/models/blog_post.rb
+++ b/app/models/blog_post.rb
@@ -6,6 +6,7 @@ class BlogPost < ActiveRecord::Base
acts_as_indexed :fields => [:title, :body]
validates :title, :presence => true, :uniqueness => true
+ validates :body, :presence => true
has_friendly_id :title, :use_slug => true
diff --git a/features/support/factories/blog_categories.rb b/features/support/factories/blog_categories.rb
index 10b51b5..eaf025c 100644
--- a/features/support/factories/blog_categories.rb
+++ b/features/support/factories/blog_categories.rb
@@ -1,4 +1,4 @@
Factory.define(:blog_category) do |f|
- f.title "Shopping"
+ f.sequence(:title) { |n| "Shopping #{n}" }
f.posts {|p| [p.association(:post)]}
end
diff --git a/features/support/factories/blog_posts.rb b/features/support/factories/blog_posts.rb
index 8dd3f54..24436b8 100644
--- a/features/support/factories/blog_posts.rb
+++ b/features/support/factories/blog_posts.rb
@@ -1,4 +1,6 @@
Factory.define(:post, :class => BlogPost) do |f|
- f.title "Top Ten Shopping Centers in Chicago"
+ f.sequence(:title) { |n| "Top #{n} Shopping Centers in Chicago" }
f.body "These are the top ten shopping centers in Chicago. You're going to read a long blog post about them. Come to peace with it."
+ f.draft false
+ f.published_at Time.now
end
diff --git a/spec/models/blog_posts_spec.rb b/spec/models/blog_posts_spec.rb
index 179197d..ef9d700 100644
--- a/spec/models/blog_posts_spec.rb
+++ b/spec/models/blog_posts_spec.rb
@@ -2,15 +2,158 @@ require 'spec_helper'
Dir[File.expand_path('../../../features/support/factories/*.rb', __FILE__)].each{|factory| require factory}
describe BlogPost do
- context "wiring up" do
+ describe "validations" do
+ before(:each) do
+ @attr = { :title => "RefineryCMS", :body => "Some random text ..." }
+ end
+
+ it "requires title" do
+ BlogPost.new(@attr.merge(:title => "")).should_not be_valid
+ end
+
+ it "won't allow duplicate titles" do
+ BlogPost.create!(@attr)
+ BlogPost.new(@attr).should_not be_valid
+ end
+
+ it "requires body" do
+ BlogPost.new(@attr.merge(:body => "")).should_not be_valid
+ end
+ end
+
+ describe "comments association" do
+ before(:each) do
+ @blog_post = Factory(:post)
+ end
+
+ it "have a comments attribute" do
+ @blog_post.should respond_to(:comments)
+ end
+
+ it "destroys associated comments" do
+ Factory(:blog_comment, :blog_post_id => @blog_post)
+ @blog_post.destroy
+ BlogComment.find_by_blog_post_id(@blog_post).should be_nil
+ end
+ end
+
+ describe "categories association" do
+ before(:each) do
+ @blog_post = Factory(:post)
+ end
+
+ it "have categories attribute" do
+ @blog_post.should respond_to(:categories)
+ end
+ end
+
+ describe "by_archive scope" do
+ it "returns all posts from specified month" do
+ blog_post1 = Factory(:post, :published_at => Time.now - 2.minutes)
+ blog_post2 = Factory(:post, :published_at => Time.now - 1.minute)
+ Factory(:post, :published_at => Time.now - 2.months)
+ date = "#{Time.now.month}/#{Time.now.year}"
+ BlogPost.by_archive(Time.parse(date)).count.should == 2
+ BlogPost.by_archive(Time.parse(date)).should == [blog_post2, blog_post1]
+ end
+ end
+
+ describe "all_previous scope" do
+ it "returns all posts from previous months" do
+ blog_post1 = Factory(:post, :published_at => Time.now - 1.month)
+ 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.should == [blog_post2, blog_post1]
+ end
+ end
- before(:each) do
- @post = Factory(:post)
- end
+ describe "live scope" do
+ it "returns all posts which aren't in draft and pub date isn't in future" do
+ blog_post1 = Factory(:post, :published_at => Time.now - 2.minutes)
+ blog_post2 = Factory(:post, :published_at => Time.now - 1.minute)
+ Factory(:post, :draft => true)
+ Factory(:post, :published_at => Time.now + 1.minute)
+ BlogPost.live.count.should == 2
+ BlogPost.live.should == [blog_post2, blog_post1]
+ end
+ end
+
+ describe "next scope" do
+ it "returns next article based on given article" do
+ blog_post1 = Factory(:post)
+ blog_post2 = Factory(:post, :published_at => Time.now + 1.minute)
+ BlogPost.next(blog_post1).should == [blog_post2]
+ end
+ end
- it "saves to the database" do
- @post.should_not be_nil
- end
+ describe "previous scope" do
+ it "returns previous article based on given article" do
+ blog_post1 = Factory(:post)
+ blog_post2 = Factory(:post, :published_at => Time.now + 1.minute)
+ BlogPost.next(blog_post1).should == [blog_post2]
+ end
+ end
+
+ describe "#live?" do
+ it "returns true if post is not in draft and it's published" do
+ Factory(:post).live?.should be_true
+ end
+ it "returns false if post is in draft" do
+ Factory(:post, :draft => true).live?.should be_false
end
+
+ it "returns false if post pub date is in future" do
+ Factory(:post, :published_at => Time.now + 1.minute).live?.should be_false
+ end
+ end
+
+ describe "#next" do
+ it "returns next article when called on current article" do
+ Factory(:post)
+ blog_post = Factory(:post, :published_at => Time.now + 1.minute)
+ blog_posts = BlogPost.all
+ blog_posts.first.next.should == blog_post
+ end
+ end
+
+ describe "#prev" do
+ it "returns previous article when called on current article" do
+ Factory(:post)
+ blog_post = Factory(:post, :published_at => Time.now - 1.minute)
+ blog_posts = BlogPost.all
+ blog_posts.first.prev.should == blog_post
+ end
+ end
+
+ describe "#category_ids=" do
+ before(:each) do
+ @blog_post = Factory(:post)
+ @cat1 = Factory(:blog_category, :id => 1)
+ @cat2 = Factory(:blog_category, :id => 2)
+ @cat3 = Factory(:blog_category, :id => 3)
+ @blog_post.category_ids = [1,2,"","",3]
+ end
+
+ it "rejects blank category ids" do
+ @blog_post.categories.count.should == 3
+ end
+
+ it "returns array of categories based on given ids" do
+ @blog_post.categories.should == [@cat1, @cat2, @cat3]
+ end
+ end
+
+ describe ".comments_allowed?" do
+ it "returns true if comments_allowed setting is set to true" do
+ BlogPost.comments_allowed?.should be_true
+ end
+
+ # TODO: fix this test
+ # it "returns false if comments_allowed setting is set to false" do
+ # RefinerySetting.set(:comments_allowed, false)
+ # BlogPost.comments_allowed?.should be_false
+ # end
+ end
end