DEV Community

Augusts Bautra
Augusts Bautra

Posted on

Simple testing of Rails cache with RSpec

Turns out selectively enabling and cleaning up cache for select tests is easy. Testing with caching is needed where the functionality relies heavily on cache, like work with 3rd party API responses.

Here's a simple context:

RSpec.shared_context("with cache", :with_cache) do
  # Inclusion of this context enables and mocks cache.
  # Allows Rails.cache to behave just like it would on dev and prod!

  let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }

  before do
    allow(Rails).to receive(:cache).and_return(memory_store)
    Rails.cache.clear
  end
end
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jonathanfrias profile image
Jonathan Frias

This is an awesome snippet! I also added a quick check before the actual mock for my own sanity.

if Rails.cache.class != ActiveSupport::Cache::NullStore
  raise "Cache is already enabled. Remove this context or convert this to 'without cache'"
end

Enter fullscreen mode Exit fullscreen mode