1# frozen_string_literal: true 2 3require 'spec_helper' 4 5RSpec.describe BaseCountService, :use_clean_rails_memory_store_caching do 6 let(:service) { described_class.new } 7 8 describe '#relation_for_count' do 9 it 'raises NotImplementedError' do 10 expect { service.relation_for_count }.to raise_error(NotImplementedError) 11 end 12 end 13 14 describe '#count' do 15 it 'returns the number of values' do 16 expect(service) 17 .to receive(:cache_key) 18 .and_return('foo') 19 20 expect(service) 21 .to receive(:uncached_count) 22 .and_return(5) 23 24 expect(service.count).to eq(5) 25 end 26 end 27 28 describe '#uncached_count' do 29 it 'returns the uncached number of values' do 30 expect(service) 31 .to receive(:relation_for_count) 32 .and_return(double(:relation, count: 5)) 33 34 expect(service.uncached_count).to eq(5) 35 end 36 end 37 38 describe '#refresh_cache' do 39 it 'refreshes the cache' do 40 allow(service) 41 .to receive(:cache_key) 42 .and_return('foo') 43 44 allow(service) 45 .to receive(:uncached_count) 46 .and_return(4) 47 48 service.refresh_cache 49 50 expect(Rails.cache.fetch(service.cache_key, raw: service.raw?)).to eq(4) 51 end 52 end 53 54 describe '#delete_cache' do 55 it 'deletes the cache' do 56 allow(service) 57 .to receive(:cache_key) 58 .and_return('foo') 59 60 allow(service) 61 .to receive(:uncached_count) 62 .and_return(4) 63 64 service.refresh_cache 65 service.delete_cache 66 67 expect(Rails.cache.fetch(service.cache_key, raw: service.raw?)).to be_nil 68 end 69 end 70 71 describe '#raw?' do 72 it 'returns false' do 73 expect(service.raw?).to eq(false) 74 end 75 end 76 77 describe '#cache_key' do 78 it 'raises NotImplementedError' do 79 expect { service.cache_key }.to raise_error(NotImplementedError) 80 end 81 end 82 83 describe '#cache_options' do 84 it 'returns the default in options' do 85 expect(service.cache_options).to eq({ raw: false }) 86 end 87 end 88end 89