Rails/RSpec toggle cache for a single test

前端 未结 1 1890
傲寒
傲寒 2021-02-15 10:40

So in my app I can disable the cache for all tests, which would be ideal, but apparently there are a number of legacy tests that rely on the cache being functional. Is there a w

相关标签:
1条回答
  • 2021-02-15 11:21

    in spec_helper.rb

    RSpec.configure do |config|
      config.before(:example, disable_cache: true) do
        allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::NullStore.new)
      end
    
      config.after(:example, disable_cache: true) do
        allow(Rails).to receive(:cache).and_call_original
      end
    end
    

    in xxx_spec.rb

    RSpec.describe "a group without matching metadata" do
      it "does not run the hook" do
         puts Rails.cache.class
      end
    
      it "runs the hook for a single example with matching metadata", disable_cache: true do
         puts Rails.cache.class
      end
    end
    

    https://www.relishapp.com/rspec/rspec-core/docs/hooks/filters

    0 讨论(0)
提交回复
热议问题