Posted about 5 years 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 Archive 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

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Avatar
Emanuel De
Last edit:
about 4 years 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 short-lived cookies to improve usability.
Accept or learn more