If you're in the default testing environment your rails cache is
ActiveSupport::Cache::NullStore
which will always succeed but it
doesn't actually store or return anything.
There's an almost completely undocumented method called
with_local_cache
on NullStore
that lets you run a block with a
MemoryStore
instead of a NullStore
for the duration of the block.
This happens because it prepends
the
ActiveSupport::Cache::Strategy::LocalCache
class. This lets you test
one or two things that require caching without enabling it on all
tests and slowing everything down.
Rails.cache.class.name
# => "ActiveSupport::Cache::NullStore"
Rails.cache.write("a", 3)
# => true
Rails.cache.read("a")
# => nil
Rails.cache.with_local_cache do
Rails.cache.write("a", 5)
p Rails.cache.read("a")
end
5
# => 5