测试之间清除Rails.cache吗? [英] is Rails.cache purged between tests?

查看:71
本文介绍了测试之间清除Rails.cache吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Rails 3.2应用程序中使用 Rails.cache 缓存ID /路径映射。在某些机器上,它可以正常工作,但在其他机器上,值是错误的。原因很难追踪,所以我对 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?

我的缓存存储区的配置是:

My cache store is configuration is:

#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

添加:

before(:each) do
  Rails.cache.clear
end

在每个规范之前清除缓存。

to have the cache cleared before each spec.

您可以将其放在 spec / spec_helper.rb 中c $ c> RSpec.configure 块可将其全局应用(建议将其分散在每个spec文件或大小写中)。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆