Why do I see test environment data in my Rails development cache?

I am trying to cache a class variable like this:

Rails.cache.write("@@page_types", @@page_types) 

This method is called inside the class, which I called PageTypes.

If I run the rails console and do:

 Rails.cache.write("@@page_types", nil) Rails.cache.read("@@page_types") 

I get zero. I leave the console open and do it in another window:

rake test: units

When the tests are finished, I will return to the rails console window and do

 Rails.cache.read("@@page_types") 

It returns an array of my test pages! I am sure they are from my db test, because all models have super-high identifiers, while my dev data has very low values.

I suppose I could add Rails.env to the cache keys, but it looks like the two caches shouldn't mix ...

+4
source share
1 answer

Define a different cache server for the test environment. Memory_store should be ideal for unit tests.

 ActionController::Base.cache_store = :memory_store 

in config / environment / test.rb:

 config.cache_store = :memory_store 
+7
source

All Articles