Forum: Ruby on Rails Disable Rails caching in test environment?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A99ccfa84810c6016a9d3dc3429a2e00?d=identicon&s=25 Tony Primerano (primerano)
on 2010-09-02 15:21
(Received via mailing list)
Is there a way to make Rails.cache.fetch always execute the code block
in the test environment?  (disable caching)

For example in my test console on Rails 3

ruby-1.9.2-rc2 > Rails.cache.fetch('foo') {'bar'}
 => "bar"
ruby-1.9.2-rc2 > Rails.cache.fetch('foo') {'bar44'}
 => "bar"

Caching is on.  Using filestore

ruby-1.9.2-rc2 > Rails.cache
 => #<ActiveSupport::Cache::FileStore

Is there a ActiveSupport::Cache::Store implementation that doesn't
cache?   I'm assuming I'm just missing something trivial since this is
easily disabled for page/action/fragment caching with

config.action_controller.perform_caching = false

I have...
ruby-1.9.2-rc2 >
Rails.application.config.action_controller.perform_caching
 => false

What am I missing?  Thanks
Tony
A99ccfa84810c6016a9d3dc3429a2e00?d=identicon&s=25 Tony Primerano (primerano)
on 2010-09-02 16:57
(Received via mailing list)
2 solutions...

If you want caching on in test mode (good way to test it is doing what
you expect) you can simply clear the cache between tests.

  def setup
     Rails.cache.clear
  end

Or you can write a cache that doesn't store anything.

put this in lib/active_support/cache/no_store.rb

module ActiveSupport
  module Cache
    class NoStore < Store
      def read_entry(name, options)
        return nil
      end
      def write_entry(name, value, options)
      end
      def delete_entry(name, options)
      end
    end
  end
end

then add config.cache_store = :no_store to your test.rb file.
6e6e22d81a1f4f394f62301be40c2e20?d=identicon&s=25 Joshua Pinter (Guest)
on 2013-08-09 12:35
(Received via mailing list)
You can also use the following in your environment configuration:

config.cache_store = :null_store

Cheers,

JP
This topic is locked and can not be replied to.