Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Created October 17, 2019 11:50
Show Gist options
  • Save rebelweb/844500f4ca103aac69da1a7387993f86 to your computer and use it in GitHub Desktop.
Save rebelweb/844500f4ca103aac69da1a7387993f86 to your computer and use it in GitHub Desktop.
RSpec.describe UpdateCounterCachesJob, type: :job do
it 'updates all cache columns when they are out of sync' do sql = <<-SQL update categories SET articles_count = 5 SQL
category = create(:category)
ActiveRecord::Base.connection.execute(sql)
category.reload
expect(category.articles_count).to eq(5)
UpdateCounterCachesJob.perform_now
category.reload
expect(category.articles_count).to eq(0)
end
it 'updates an individual model\'s cache columns' do
sql = <<-SQL
update categories
SET articles_count = 5
SQL
category = create(:category)
ActiveRecord::Base.connection.execute(sql)
category.reload
expect(category.articles_count).to eq(5)
UpdateCounterCachesJob.perform_now('category')
category.reload
expect(category.articles_count).to eq(0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment