Created
March 5, 2014 15:14
-
-
Save ozgg/9369182 to your computer and use it in GitHub Desktop.
Changing counters after create
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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