ruby-on-rails - Rails.cache.fetch 在测试中产生错误
问题描述
我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
我的缓存方法需要一个过程并执行此操作
def use_cached(evaluate_value_proc, key, cache_options)
Rails.cache.fetch(key, cache_options) do
evaluate_value_proc.call
end
end
在测试中,我希望缓存会丢失,并且块会被再次调用。这里发生了什么?
谢谢
解决方案
我通过模拟返回的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)
推荐阅读
- reactjs - 如何在 React 测试中点击两次相同的位置?
- delphi - 防止枚举类型数组的调试信息出现在 EXE 中
- c - 在c编程中生成2个数字之间的数字范围时如何显示负值
- python - Pyplot 标题垂直位置
- html - 可缩放的全屏 BG 视频
- android - Android 项目中带有 Gradle 插件的模块
- python - Python DataFrame - 在第二个数据帧中比较是否满足条件进行计数
- javascript - 在 Firebase 云函数中计算子项
- javascript - 当查询结果需要在发送到客户端之前进行操作时,如何从 Mongodb 快速访问大量数据
- c++ - 在编辑控件中使用上标和下标