Posted over 1 year ago. Visible to the public.

How to: Rails cache for individual rspec tests

Rails default config uses the ActiveSupport::Cache::NullStore and disables controller caching for all environments except production:

Copy
config.action_controller.perform_caching = false config.cache_store = :null_store

If you want to test caching you have at least two possibilities:

  1. Enable caching for every test (not covered by this card and straightforward)
  2. Enable caching for individual test

Enable caching for individual test (file cache)

1. Leave the default configuration
2. Add a caching helper which gives you a unique file caching path for parallel tests:

Copy
module CachingHelpers def file_caching_path path = "tmp/test#{ENV['TEST_ENV_NUMBER']}/cache" FileUtils::mkdir_p(path) path end end RSpec.configure do |config| config.include CachingHelpers end

3. Now stub the Rails cache and clear it before each example:

Copy
RSpec.describe SomeClass let(:file_cache) { ActiveSupport::Cache.lookup_store(:file_store, file_caching_path) } let(:cache) { Rails.cache } before do allow(Rails).to receive(:cache).and_return(file_cache) Rails.cache.clear end it do expect(cache.exist?('some_key')).to be(false) cache.write('some_key', 'test') expect(cache.exist?('some_key')).to be(true) end end

Enable caching for individual test (memory store)

1. Leave the default configuration
2. Now stub the Rails cache and clear it before each example:

Copy
RSpec.describe SomeClass # memory store is per process and therefore no conflicts in parallel tests let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } let(:cache) { Rails.cache } before do allow(Rails).to receive(:cache).and_return(memory_store) Rails.cache.clear end it do expect(cache.exist?('some_key')).to be(false) cache.write('some_key', 'test') expect(cache.exist?('some_key')).to be(true) end end

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Emanuel De
Last edit:
11 months ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more