轨道操作缓存 RSpec 测试失败



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

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

当按上述顺序运行测试时,最后一个测试将失败,因为最后一个预期中不存在cache_store。 我很困惑为什么无缓存测试会影响缓存测试。 有谁知道出了什么问题?

如果您将 spec_helper.rb 随机测试顺序转换为 true,这是有意义的,因为您没有撤消"ActionController::Base.perform_caching = false"设置。

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

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

确保您具有:

config.action_controller.perform_caching = true

environment/test.rb.

否则 - 我注意到了超级奇怪的事情。当我只运行请求测试(spring rspec spec/requests describe '..' type: :request)时,缓存有效,但是如果我使用 rspec spec 运行所有内容,则相同的测试失败。

最新更新