Skip to content

Instantly share code, notes, and snippets.

@davidimoore
Created February 20, 2017 23:42
Show Gist options
  • Select an option

  • Save davidimoore/29fd6309969c7fc63a84a6a42eabeaef to your computer and use it in GitHub Desktop.

Select an option

Save davidimoore/29fd6309969c7fc63a84a6a42eabeaef to your computer and use it in GitHub Desktop.
describe "counter caches" do
it "increments associations when created" do
user = User.create(first_name: "David", last_name: "Moore", email:"david@moore.com")
#creating a thread with the user
user.forum_threads.create(name:"First thread")
expect(user.forum_threads_count).to eq 1
#create a thread with its own class and associating it to the user
ForumThread.create(name: "Second thread", user: user)
expect(user.forum_threads_count).to eq 2
#destroy a forum thread and check if form_threads_count is updated
ForumThread.last.destroy
expect(user.forum_threads_count).to eq 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment