Skip to content

Latest commit

 

History

History
41 lines (29 loc) · 1.09 KB

use-specific-cache-store-in-a-single-test.md

File metadata and controls

41 lines (29 loc) · 1.09 KB

Use Specific Cache Store In A Single Test

It is generally recommended to use :null_store as the default cache store across your test suite. This is defined in config/environments/test.rb.

  config.cache_store = :null_store

This generally simplifies tests by avoiding confusing stateful scenarios.

If there is a test where you want to observe specific caching behavior, then you'll need to temporarily swap that for another store.

One way to do that is by mocking the cache with MemoryStore in a before block.

describe '#caching_feature' do
  # use MemoryStore cache for these tests
  before do
    allow(Rails)
      .to receive(:cache)
      .and_return(ActiveSupport::Cache::MemoryStore.new)
  end

  it 'does caching' do
    thing = Thing.caching_feature(1)

    expect(thing.value).to eq true

    thing.update(value: false)

    thing = Thing.caching_feature(1)

    expect(thing.value).to eq true
  end
end

One source and another