Rails.cache.fetch在测试中产生错误

问题描述 投票:1回答:1

在运行涉及ActionView::Template::Error: RSpec::Mocks::Double#marshal_dump returned same class instance的测试时得到Rails.cache.fetch。在我的config / environments / test.rb我禁用缓存(我认为):

config.cache_store = :null_store
config.action_controller.perform_caching = false

我的缓存方法采用proc并执行此操作

def use_cached(evaluate_value_proc, key, cache_options)
  Rails.cache.fetch(key, cache_options) do
    evaluate_value_proc.call
  end
end

在测试中,我希望缓存丢失,而只是再次调用该块。这是怎么回事?

谢谢

ruby-on-rails caching rspec
1个回答
0
投票

我通过模拟exist?memory_store方法返回false,以便fetch方法将调用block的方式测试了这些情况

allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache.lookup_store(:memory_store))
allow_any_instance_of(ActiveSupport::Cache::MemoryStore).to receive(:exist?).and_return(false)
© www.soinside.com 2019 - 2024. All rights reserved.