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.
Changing counters after create
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment