Skip to content

Instantly share code, notes, and snippets.

@aldesantis
Created June 2, 2016 13:19
Show Gist options
  • Save aldesantis/8553209a509d4177b77cce5ae982231c to your computer and use it in GitHub Desktop.
Save aldesantis/8553209a509d4177b77cce5ae982231c to your computer and use it in GitHub Desktop.
RSpec.describe Review do
let(:book) { Book.create! }
subject { Review.new(book: book) }
it "increments the book's reviews_count when saving" do
expect {
subject.save!
}.to change(book, :reviews_count).by(1)
end
it "decrements the book's reviews_count when destroying" do
subject.save!
expect {
subject.destroy!
}.to change(book, :reviews_count).by(-1)
end
it "moves the entry when changing book" do
book2 = Book.create!
subject.save!
expect {
subject.update! book: book2
}.to change(book, :reviews_count).by(-1) && change(book2, :reviews_count).by(1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment