Rails caching test

I have a spec that checks for action caching when caching is disabled and when caching is enabled. It seems that the order in which the tests are run 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 the tests are run in the above order, the last test fails because the cache_store does not exist in the last wait. I don't understand why the caching test doesn't affect the caching test. Does anyone know what is wrong?

+3


source to share


2 answers


If you have a random order of the spec_helper.rb validation that is true, it makes sense because you are not doing the "ActionController :: Base.perform_caching = false" setting.

The recommended way to write caching tests is to run before (: each) and after (: each) setting and configuring caching.



Since you are testing these parameters, if you turn it on, remember to turn it off for the rest of the test and vice versa. Your tests will be more atomic.

0


source


Make sure you have:

config.action_controller.perform_caching = true

      



In environment/test.rb

.

Else - I noticed a super weird thing. The caching worked when I only ran test requests ( spring rspec spec/requests

describe '..' type: :request

), but the same tests failed if I ran everything with rspec spec

.

0


source







All Articles