aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/refinery/blog/category_spec.rb
blob: 577cb9d259de194de18a7ce99bc3bcc5d842abdf (plain) (blame)
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
require 'spec_helper'

module Refinery
  module Blog
    describe Category, type: :model do
      let(:category) { FactoryGirl.create(:blog_category) }
      let(:refinery_user) { FactoryGirl.create(:refinery_user) }

      describe "validations" do
        it "requires title" do
          expect(FactoryGirl.build(:blog_category, :title => "")).not_to be_valid
        end

        it "won't allow duplicate titles" do
          expect(FactoryGirl.build(:blog_category, :title => category.title)).not_to be_valid
        end
      end

      describe "blog posts association" do
        it "has a posts attribute" do
          expect(category).to respond_to(:posts)
        end

        it "returns posts by published_at date in descending order" do
          first_post = category.posts.create!({ :title => "Breaking News: Joe Sak is hot stuff you guys!!",
                                                :body => "True story.",
                                                :published_at => Time.now.yesterday,
                                                :author => refinery_user })

          latest_post = category.posts.create!({ :title => "parndt is p. okay",
                                                 :body => "For a Kiwi.",
                                                 :published_at => Time.now,
                                                 :author => refinery_user })

          expect(category.posts.newest_first.first).to eq(latest_post)
        end

      end

      describe "#post_count" do
        it "returns post count in category" do
          2.times do
            category.posts << FactoryGirl.create(:blog_post)
          end
          expect(category.post_count).to eq(2)
        end
      end
    end
  end
end