49

We cache id/path mapping using Rails.cache in a Rails 3.2 app. On some machines it works OK, but on the others values are wrong. The cause is hard to track so I have some questions about the Rails.cache itself. Is it purged between tests? Is it possible that values cached in development mode is used in test mode? If it's not purged, how could I do it before running specs?

My cache store is configuration is:

#in: config/environments/development.rb
config.cache_store = :memory_store, {:size => 64.megabytes}

#in: config/environments/production.rb
# config.cache_store = :mem_cache_store
mrzasa
  • 22,895
  • 11
  • 56
  • 94

2 Answers2

71

A more efficient (and easier) method is to set the test environment's cache to use NullStore:

# config/environments/test.rb:
config.cache_store = :null_store

The NullStore ensures that nothing will ever be cached.

For instance in the code below, it will always fall through to the block and return the current time:

Rails.cache.fetch('time') { Time.now }

Also see the Rails Caching guide: http://guides.rubyonrails.org/caching_with_rails.html#activesupport-cache-nullstore

jaustin
  • 925
  • 1
  • 7
  • 9
61

Add:

before(:all) do
  Rails.cache.clear
end

to have the cache cleared before each spec file is run.

Add:

before(:each) do
  Rails.cache.clear
end

to have the cache cleared before each spec.

You can put this inside spec/spec_helper.rb within the RSpec.configure block to have it applied globally (recommended over scattering it per spec file or case).

RSpec by default does not clear that cache automatically.

approxiblue
  • 6,982
  • 16
  • 51
  • 59
mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • 1
    Why don't you just disable the cache on the tests? – Fran Nov 12 '14 at 09:55
  • @Francisco: because it doesn't prevent the cache to fetched. `config.action_controller.perform_caching = false` seems to just prevent *filling* the cache. – Antoine Lizée Jun 05 '15 at 13:24
  • 7
    If we disable the cache on the tests, how we can test the cached responses itself? Cache validation, I believe, is one of the hardest part of programming, so if we have cached responses, we *should* test them. – Psylone Apr 18 '16 at 16:25
  • You should be mocking the cache requests and responses. Using the actual cache needlessly couples your test code to implementation. – Epigene Sep 25 '18 at 08:12
  • 2
    Just a side not, (for RSpec 3.8 at least), to put this in `spec/spec_helper.rb`, you'll need `config.before(:each) do blah end` instead of `before(:each) do blah end` – Yi Zeng Dec 28 '18 at 12:33