简体   繁体   English

Rails操作缓存RSpec测试失败

[英]Rails action caching RSpec test fails

I have a spec that tests action caching when caching is disabled and when caching is enabled. 我有一个规范,在禁用缓存和启用缓存时测试操作缓存。 It seems like the order of test execution affects whether they pass or not. 看起来测试执行的顺序会影响它们是否通过。

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

When tests are run in the above order the last test fails because the cache_store does not exist in the last expectation. 当以上述顺序运行测试时,最后一次测试失败,因为cache_store在最后一次期望中不存在。 I'm stumped on why the no caching test is affecting the caching test. 我很难理解为什么没有缓存测试会影响缓存测试。 Does anyone know what is wrong? 有谁知道什么是错的?

If you have spec_helper.rb random test order turned to true, it makes sense since you're not un-doing your "ActionController::Base.perform_caching = false" setting. 如果你有spec_helper.rb随机测试顺序变为true,那么它是有意义的,因为你没有取消你的“ActionController :: Base.perform_caching = false”设置。

The recommended way to write caching tests is to do a before(:each) and after(:each) setting the caching settings on and off. 编写缓存测试的推荐方法是执行on(:each)和after(:each)设置缓存设置的开启和关闭。

Since you're testing these settings, if you turn it on, remember to turn it off before the end of the test, and vice-versa. 由于您正在测试这些设置,因此如果您打开它,请记住在测试结束前将其关闭,反之亦然。 Your tests will be more atomic. 你的测试将更加原子化。

Make sure that you have: 确保你有:

config.action_controller.perform_caching = true

In environment/test.rb . environment/test.rb

Else - i noticed super strange thing. 否则 - 我注意到了超级奇怪的事情。 Caching worked when I run only requests tests ( spring rspec spec/requests describe '..' type: :request ), but same tests failed if I run everything with rspec spec . 当我只运行请求测试时,缓存工作( spring rspec spec/requests describe '..' type: :request ),但如果我用rspec spec运行所有内容,则相同的测试失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM