Rails fragment cache testing with RSpec

前端 未结 3 472
生来不讨喜
生来不讨喜 2021-02-04 15:22

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.

I\'m fragment caching in the view usin

3条回答
  •  伪装坚强ぢ
    2021-02-04 15:48

    Here is what I've used in my specs with caching enabled in my config/environments/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
    

提交回复
热议问题