Sha256: 1f47040d18c423fc1753e7f6e2271a9d3e5f7be555c6681b073cb81d9717b2ee

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

require File.dirname(__FILE__) + '/../spec_helper'

module Enki
  describe CommentActivity, '#comments' do

    def valid_comment_attributes(extra = {})
      {
        :author       => 'Don Alias',
        :author_url   => "me",
        :author_email => "me@fake.com",
        :body         => 'This is a comment',
        :post         => Post.create!(:title => 'My Post', :body => "body", :tag_list => "ruby")
      }.merge(extra)
    end

    context "find recent comments" do
      before :each do
        @comments = []
        (1..10).each do |n|
          @comments << Comment.create!(valid_comment_attributes(:created_at => Time.now - n * (60 * 60 * 24)))
        end
      end

      it "should have the comment activity sorted by when they were created" do
        comment_activity = CommentActivity.find_recent
        comment_activity.first.post.should == @comments.first.post
      end

      it do
        comment_activity = CommentActivity.find_recent
        comment_activity.should have_exactly(5).posts
      end

      it "should not return repeated posts" do
        comment = Comment.create! valid_comment_attributes(:post => Post.first, :created_at => Time.now)
        comment_activity = CommentActivity.find_recent
        comment_activity.select{|a| a.post == comment.post}.size.should == 1
      end

    end

    it 'finds the 5 most recent approved comments for the post' do
      ret = [mock_model(Comment)]
      comments = []
      comments.should_receive(:find_recent).with(hash_including(:limit => 5)).and_return(ret)
      post = mock_model(Post)
      post.stub!(:approved_comments).and_return(comments)
      CommentActivity.new(post).comments.should == ret
    end

    it 'is memoized to avoid excess hits to the DB' do
      post = mock_model(Post)
      activity = CommentActivity.new(post)

      post.should_receive(:approved_comments).once.and_return(mock('stub').as_null_object)
      2.times { activity.comments }
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
enki-engine-0.0.5 spec/models/comment_activity_spec.rb
enki-engine-0.0.4 spec/models/comment_activity_spec.rb
enki-engine-0.0.3 spec/models/comment_activity_spec.rb
enki-engine-0.0.2 spec/models/comment_activity_spec.rb