Rails/RSpec 为单个测试切换缓存
Rails/RSpec toggle cache for a single test
所以在我的应用程序中,我可以为所有测试禁用缓存,这将是理想的,但显然有许多依赖于缓存功能的遗留测试。有没有办法为单个 RSpec 测试启用 Rails 缓存?
类似于:
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-core/docs/hooks/filters
所以在我的应用程序中,我可以为所有测试禁用缓存,这将是理想的,但显然有许多依赖于缓存功能的遗留测试。有没有办法为单个 RSpec 测试启用 Rails 缓存?
类似于:
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-core/docs/hooks/filters