Search code examples
ruby-on-railscachingdalli

How to setup dalli cache in test environment?


I'm going to use Dalli cache as key-value store.

Usually in production and development environments we have line

config.cache_store = :dalli_store

so then we can use Rails.cache construction to read from and write to cache.

But in the test environment usually, we don't have this config line.

What is the right way to set up a cache in a test environment in purpose to test my storing logic?

P.S. I'm using Linux(Ubuntu)


Solution

  • dalli is a client for the caching service (memcached) set it globally whatever the environment, ie in your config/application.rb

    config.cache_store = :dalli_store
    

    caching being deactivated in the test environment is a common approach, check config/environments/test.rb

    config.action_controller.perform_caching = false
    

    so you can enable it for the test environment, but it could lead to some weird conflicts best is probably to enable it on the go for a specific specs only:

    before do # enable caching
      @caching_state = ActionController::Base.perform_caching
      ActionController::Base.perform_caching = true
    end
    
    after do # disable caching
      ActionController::Base.perform_caching = @caching_state
    end