Rails.cache 在测试之间被清除了吗? [英] is Rails.cache purged between tests?
问题描述
我们在 Rails 3.2 应用程序中使用 Rails.cache
缓存 id/path 映射.在某些机器上它工作正常,但在其他机器上值是错误的.原因很难追踪,所以我对 Rails.cache
本身有一些疑问.它在测试之间被清除了吗?在开发模式下缓存的值是否有可能在测试模式下使用?如果没有清除,在运行规范之前我怎么做?
We cache id/path mapping using Rails.cache
in a Rails 3.2 app. On some machines it works OK, but on the others values are wrong. The cause is hard to track so I have some questions about the Rails.cache
itself. Is it purged between tests? Is it possible that values cached in development mode is used in test mode? If it's not purged, how could I do it before running specs?
我的缓存存储配置是:
#in: config/environments/development.rb
config.cache_store = :memory_store, {:size => 64.megabytes}
#in: config/environments/production.rb
# config.cache_store = :mem_cache_store
推荐答案
添加:
before(:all) do
Rails.cache.clear
end
在运行每个规范文件之前清除缓存.
to have the cache cleared before each spec file is run.
添加:
before(:each) do
Rails.cache.clear
end
在每个规范之前清除缓存.
to have the cache cleared before each spec.
您可以将其放在 spec/spec_helper.rb
中的 RSpec.configure
块中,以使其全局应用(建议在每个规范文件或案例中分散它).
You can put this inside spec/spec_helper.rb
within the RSpec.configure
block to have it applied globally (recommended over scattering it per spec file or case).
默认情况下,RSpec 不会自动清除该缓存.
RSpec by default does not clear that cache automatically.
这篇关于Rails.cache 在测试之间被清除了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!