Ruby on rails Rails操作缓存RSpec测试失败

Ruby on rails Rails操作缓存RSpec测试失败,ruby-on-rails,rspec,action-caching,Ruby On Rails,Rspec,Action Caching,我有一个规范,在禁用缓存和启用缓存时测试动作缓存。似乎测试执行的顺序会影响它们是否通过 it "should not cache the index page when we're not caching" do ActionController::Base.perform_caching = false HomeController.caches_action :index Rails.cache.clear ActionController::Base.cac

我有一个规范,在禁用缓存和启用缓存时测试动作缓存。似乎测试执行的顺序会影响它们是否通过

it "should not cache the index page when we're not caching" do
    ActionController::Base.perform_caching = false
    HomeController.caches_action :index
    Rails.cache.clear
    ActionController::Base.cache_store.exist?(:index_cache_path).should be_false
    get :index
    ActionController::Base.cache_store.exist?(:index_cache_path).should be_false
end

it "should cache the index page when we're caching" do
    ActionController::Base.perform_caching = true
    HomeController.caches_action :index
    Rails.cache.clear
    ActionController::Base.cache_store.exist?(:index_cache_path).should be_false
    get :index
    ActionController::Base.cache_store.exist?(:index_cache_path).should be_true
end

当按上述顺序运行测试时,最后一个测试失败,因为缓存存储在最后一个预期中不存在。我不明白为什么无缓存测试会影响缓存测试。有人知道哪里出了问题吗?

如果将spec\u helper.rb随机测试顺序设置为true,这是有意义的,因为您没有取消“ActionController::Base.perform\u caching=false”设置

写缓存测试的推荐方法是在打开和关闭缓存设置之前(:each)和之后(:each)


由于您正在测试这些设置,如果您打开它,请记住在测试结束前关闭它,反之亦然。您的测试将更加原子化。

确保您有:

config.action_controller.perform_caching = true
环境/test.rb


我注意到了一件非常奇怪的事情。当我只运行请求测试时,缓存工作(
spring rspec spec/requests
描述“..”类型::请求
),但是,如果我使用
rspec spec

运行所有操作,相同的测试都会失败。如果添加
render\u views
,是否仍然会失败?:即使指定了render\u views,缓存测试也会失败。我注意到在缓存测试中创建get请求时没有调用ActionCacheFilter.filter。控制器测试可能不会成功为此,请使用导轨的右侧部分。您可以尝试一个视图规范或请求(集成)规范。我还想问这个测试本身是否明智,因为您测试的是Rails的行为,而不是您自己的代码。