简体   繁体   English

为什么我在开发 Rails 缓存中看到测试环境数据?

[英]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.

相关问题 如何确定我的 rails 是否在开发环境中而不是在测试环境中? - How can I determine if my rails is in the development environment and not the test environment? 为什么我在heroku上却出现此错误,而在我的Rails应用程序中却没有开发此错误? - Why am I getting this error on heroku but not in development in my rails app? Rails 和 CORS:为什么我只看到 OPTIONS 请求? - Rails and CORS: why am I only seeing the OPTIONS request? 为什么我在'rake test'期间看到输出而不是'。' 'F'或'E'(ruby 1.9.2p290,Rails 3.0.9)? - Why am I seeing output during 'rake test' other than '.' 'F' or 'E' (ruby 1.9.2p290, Rails 3.0.9)? 任何人都可以弄清楚为什么尝试在Atom中引导我的React和Rails服务器时看到此错误吗? - Can anyone figure out why I am seeing this error when trying to boot my React and Rails servers in Atom? 如何在rails开发环境中测试错误页面? - How can I test error pages in rails development environment? 为什么我的 Rails 没有在现有项目/存储库的新开发环境中运行? - Why is my Rails not running on new development environment for existing project/repo? 为什么Nginx在开发环境中为我的Rails应用提供服务的速度非常慢? - Why is nginx serving my rails app very slowly on development environment? 为什么我的Rails应用程序试图在开发环境中发送电子邮件? - Why is my Rails app trying to send an email in the development environment? 为什么我的生产环境会在Rails中吸引开发宝石? - Why is my production environment pulling in development gems in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM