[英]Why am I seeing test environment data in my development Rails cache?
I am attempting to cache a class variable like so:我正在尝试像这样缓存 class 变量:
Rails.cache.write("@@page_types", @@page_types)
This method is called within a class I have called PageTypes.此方法在我称为 PageTypes 的 class 中调用。
If I start up a rails console and do:如果我启动 Rails 控制台并执行以下操作:
Rails.cache.write("@@page_types", nil)
Rails.cache.read("@@page_types")
I get nil.我得到零。 I leave the console open and do this in another window:
我让控制台保持打开状态,然后在另一个 window 中执行此操作:
rake test:units耙式测试:单位
When the tests are over, I switch back to my rails console window and do测试结束后,我切换回我的 Rails 控制台 window 并执行
Rails.cache.read("@@page_types")
It returns an array of my test page types, I'm positive they are from my test db because the models all have super high IDs.它返回一个我的测试页面类型的数组,我很肯定它们来自我的测试数据库,因为这些模型都有超高的 ID。 while my dev data all has very low ones.
而我的开发数据都有非常低的数据。
I suppose I could append Rails.env to the cache keys, but it seems like the two caches shouldn't be mixing....我想我可以 append Rails.env 到缓存键,但似乎这两个缓存不应该混合......
Define a different cache backend for your test environment.为您的测试环境定义不同的缓存后端。 A memory_store should be perfect for unit tests.
memory_store 应该非常适合单元测试。
ActionController::Base.cache_store = :memory_store
in config/environments/test.rb:在 config/environments/test.rb 中:
config.cache_store = :memory_store
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.