Testing Rails Fragment Cache with RSpec

I feel that this is not a very well-documented topic, at least I had problems finding the best practices.

I am caching fragments in a view using cache_key:

%tbody - @employees.each do |employee| - cache employee do %tr[employee] %td= employee.name %td= employee.current_positions %td= employee.home_base %td= employee.job_classes 

Now I can add: touch => true on the: belongs_to side of my has_many associations, and this will do everything I need to update this fragment, but for my life I can hardly figure out how to check this.

Dropping: touch => true is easy and convenient, but it extends the expiration logic to a couple of places. I would like to have an RSpec request specification that goes through and checks the behavior on this, which cannot change much, but can bring all the caching requirements into one specific file describing what is going to happen.

I tried the following lines:

 require 'spec_helper' include AuthenticationMacros describe "Employee index caching" do before do Rails.cache.clear ActionController::Base.perform_caching = true login_confirmed_employee end after do ActionController::Base.perform_caching = false end specify "the employee cache is cleared when position assignments are modified" specify "the employee cache is cleared when home base assignments are modified" end 

The specs focused on Capybara's steps that went through and made updates, of course, and I thought I was on the right track. But the tests flickered in strange ways. I would change the specifications to display employee cache_key objects, and sometimes cache_keys will change, and sometimes not, sometimes specifications will pass, and sometimes not.

Is this even a good approach?

I know that SO wants to answer questions, so for starters: how can I tune and demolish this test to use caching when my test env has no default caching? In general, however, I would love to hear how you can successfully test fragment caching in your applications if you have had success with this.

EDIT

I accept cailinanne's answer as it solves the problem that I specifically ask, but I decided, however, that I do not even recommend integration testing caching if you can get away from it.

Instead of specifying a touch in my association ads, I created an observer specific to my caching needs, which deals with models directly, and I test it in isolation.

I would recommend that testing the browser of the mullite model separately also include a test to check the observed observed modes, otherwise you might drown out too much reality.

The specific answer that led me to this is given here: https://stackoverflow.com/a/212618/

+8
caching ruby-on-rails testing rspec
source share
2 answers

Let me first say that in this answer you can get more empathy than fact. I struggled with the same problems. While I was able to get reproducible results for a particular test, I found that the results varied depending on whether I was fulfilling one or more specifications, either with or without spork. Sigh.

In the end, I found that 99.9% of my problems disappeared if I just turned on caching in the test.rb file. This may seem strange, but after we thought it was β€œright” for my application. The vast majority of my tests are not at the presentation / query level, and for the few that are, does it make sense to test the same configurations as the user?

While I was struggling with this, I wrote a blog post that contained some useful test assistants for testing caching. You may find this helpful.

+7
source share

Here is what I used in my specs with caching included in my config / environment / test.rb

 require 'spec_helper' include ActionController::Caching::Fragments describe 'something/index.html.erb' do before(:each) do Rails.cache.clear render end it 'should cache my fragment example' do cached_fragment = Rails.cache.read(fragment_cache_key(['x', 'y', 'z'])) cached_fragment.should have_selector("h1") end end 
+2
source share

All Articles