has_cache_examples.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'ApplicationModel::HasCache' do
  3. describe '#cache_delete' do
  4. let(:instance) { create(described_class.name.underscore) }
  5. it 'clears cache after updating the object' do
  6. allow(instance).to receive(:cache_delete)
  7. instance.touch
  8. expect(instance).to have_received(:cache_delete)
  9. end
  10. it 'clears cache after deleting the object' do
  11. allow(instance).to receive(:cache_delete)
  12. instance.destroy
  13. expect(instance).to have_received(:cache_delete)
  14. end
  15. end
  16. end
  17. RSpec.shared_examples 'Association clears cache' do |association:, factory: nil|
  18. describe "#{association} association clears cache", aggregate_failures: true do
  19. let(:instance) { create(described_class.name.underscore) }
  20. let(:other_object) { create(factory || association.to_s.singularize) }
  21. it 'after adding an object to collection' do
  22. allow(instance).to receive(:cache_delete)
  23. allow(other_object).to receive(:cache_delete)
  24. instance.send(association) << other_object
  25. expect(instance).to have_received(:cache_delete).at_least(:once)
  26. expect(other_object).to have_received(:cache_delete).at_least(:once)
  27. end
  28. it 'after removing an object from collection' do
  29. instance.send(association) << other_object
  30. allow(instance).to receive(:cache_delete)
  31. allow(other_object).to receive(:cache_delete)
  32. instance.send(association).delete(other_object)
  33. expect(instance).to have_received(:cache_delete).at_least(:once)
  34. expect(other_object).to have_received(:cache_delete).at_least(:once)
  35. end
  36. end
  37. end