Skip to content

Instantly share code, notes, and snippets.

@ozgg
Created March 5, 2014 15:14
Show Gist options
  • Select an option

  • Save ozgg/9369182 to your computer and use it in GitHub Desktop.

Select an option

Save ozgg/9369182 to your computer and use it in GitHub Desktop.

Revisions

  1. ozgg created this gist Mar 5, 2014.
    46 changes: 46 additions & 0 deletions comment_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    require 'spec_helper'

    describe Comment do
    let(:comment) { build(:comment) }
    let(:user) { create(:user) }
    let(:dream) { create(:dream) }

    context "integrity" do
    it "has non-blank body" do
    comment.body = ' '
    comment.valid?
    expect(comment.errors).to have_key(:body)
    end

    it "is invalid without entry_id" do
    comment.valid?
    expect(comment.errors).to have_key(:entry)
    end
    end

    context "when saved" do
    before(:each) do
    comment.entry = dream
    comment.user = user
    end

    it "increments user's comments_count" do
    expect { comment.save }.to change(user, :comments_count).by(1)
    end

    it "increments entry's comments_count" do
    expect { comment.save }.to change(dream, :comments_count).by(1)
    end
    end

    context "when destroyed" do
    before(:each) do
    comment.entry = dream
    comment.user = user
    comment.save
    end

    it "decrements user's comments_count"
    it "decrements entry's comments_count"
    end
    end