Note that there are some explanatory texts on larger screens.

plurals
  1. PORails fragment cache testing with RSpec
    primarykey
    data
    text
    <p>I feel like this is a not-so-much documented topic, at least I've had a lot of trouble finding our about the best practices here.</p> <p>I'm fragment caching in the view using a cache_key:</p> <pre><code>%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 </code></pre> <p>Now I can add :touch => true on the :belongs_to side of my has_many associations and this will do everything I need to keep this fragment caching up to date, but for the life of me I'm having a hard time figuring out how to test this.</p> <p>Dropping in :touch => true is easy and convenient but it spreads the expiry logic around a couple places. I'd love to have an RSpec request spec that walks through and checks the behavior on this, something that isn't liable to change much but can bring all the caching requirements into one specific file that describes what is supposed to be occurring.</p> <p>I tried along these lines:</p> <pre><code>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 </code></pre> <p>The specs were fleshed out with the Capybara steps of going through and making the updates of course, and I thought I was quite on the right track. But the tests were flickering in weird ways. I would modify the specs to output the employee objects cache_key, and sometimes the cache_keys would change and sometimes not, sometimes the specs would pass and sometimes not.</p> <p>Is this even a good approach?</p> <p>I know SO wants questions that are answerable, so to start: how can I set up and tear down this test to use caching, when my test env does not have caching on by default? In general, however, I'd really like to hear how you might be successfully testing fragment caching in your apps if you have had success with this.</p> <p><strong>EDIT</strong></p> <p>I'm accepting cailinanne's answer as it addresses the problem that I specifically ask about, but I have decided however that I don't even recommend integration testing caching if you can get away from it.</p> <p>Instead of specifying touch in my association declarations, I've created an observer specific to my caching needs that touches models directly, and am testing it in isolation.</p> <p>I'd recommend if testing a mulit-model observer in isolation to also include a test to check the observers observed_models, otherwise you can stub out too much of reality.</p> <p>The particular answer that lead me to this is here: <a href="https://stackoverflow.com/a/33869/717365">https://stackoverflow.com/a/33869/717365</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload