Skip to content

Instantly share code, notes, and snippets.

@christopherstyles
Last active October 2, 2024 22:13
Show Gist options
  • Select an option

  • Save christopherstyles/6bf99f20579295739382f131de87744b to your computer and use it in GitHub Desktop.

Select an option

Save christopherstyles/6bf99f20579295739382f131de87744b to your computer and use it in GitHub Desktop.
Enable cache in shared example
# spec/support/enable_cache.rb
RSpec.shared_examples "with cache" do
around do |example|
original_cache_store = Rails.cache
Rails.cache = ActiveSupport::Cache::MemoryStore.new
ActionController::Base.perform_caching = true
Rails.cache.clear
example.run
# Reset the cache store after the spec
Rails.cache = original_cache_store
ActionController::Base.perform_caching = false
Rails.cache.clear
end
end
# spec/rails_helper.rb
config.include_context "with cache", :with_cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment