簡體   English   中英

Rails操作緩存RSpec測試失敗

[英]Rails action caching RSpec test fails

我有一個規范,在禁用緩存和啟用緩存時測試操作緩存。 看起來測試執行的順序會影響它們是否通過。

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”設置。

編寫緩存測試的推薦方法是執行on(:each)和after(:each)設置緩存設置的開啟和關閉。

由於您正在測試這些設置,因此如果您打開它,請記住在測試結束前將其關閉,反之亦然。 你的測試將更加原子化。

確保你有:

config.action_controller.perform_caching = true

environment/test.rb

否則 - 我注意到了超級奇怪的事情。 當我只運行請求測試時,緩存工作( spring rspec spec/requests describe '..' type: :request ),但如果我用rspec spec運行所有內容,則相同的測試失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM