DEV Community


Simple testing of Rails cache with RSpec

Augusts Bautra
Senior Rails developer from Latvia. Avid gamer. Longevity enthusiast. #keto-dude
・1 min read

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)
Enter fullscreen mode Exit fullscreen mode

Discussion (0)