单个测试的 Rails/RSpec 切换缓存 [英] Rails/RSpec toggle cache for a single test
本文介绍了单个测试的 Rails/RSpec 切换缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以在我的应用程序中,我可以禁用所有测试的缓存,这将是理想的,但显然有许多遗留测试依赖于缓存的功能.有没有办法为单个 RSpec 测试启用 Rails 缓存?
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 way to enable the Rails cache for a single RSpec test?
类似于:
before(:each) do
@cache_setting = Rails.cache.null_cache
Rails.cache.null_cache = true
end
after(:each) do
Rails.cache.null_cache = @cache_setting
end
it 'does not hit the cache' do
...
end
推荐答案
在 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
在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-核心/文档/挂钩/过滤器
这篇关于单个测试的 Rails/RSpec 切换缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文