diff --git a/spec/models/channel/driver/twitter_spec.rb b/spec/models/channel/driver/twitter_spec.rb index 001a909a1..0e2ce832e 100644 --- a/spec/models/channel/driver/twitter_spec.rb +++ b/spec/models/channel/driver/twitter_spec.rb @@ -773,7 +773,7 @@ RSpec.describe Channel::Driver::Twitter do end end - describe '#fetch', :use_vcr do + describe '#fetch', use_vcr: :time_sensitive do describe 'Twitter API authentication' do let(:consumer_credentials) do { @@ -807,9 +807,6 @@ RSpec.describe Channel::Driver::Twitter do end describe 'Twitter API activity' do - # travel back in time when VCR was recorded - before { travel_to '2020-02-06 13:37 +0100' } - it 'sets successful status attributes' do expect { channel.fetch(true) } .to change { channel.reload.attributes } diff --git a/spec/support/vcr.rb b/spec/support/vcr.rb index e805226ac..975697ddd 100644 --- a/spec/support/vcr.rb +++ b/spec/support/vcr.rb @@ -20,24 +20,6 @@ VCR.configure do |config| end end -module VCRHelper - def self.auto_record(example) - spec_path = Pathname.new(example.file_path).realpath - cassette_path = spec_path.relative_path_from(Rails.root.join('spec')).sub(/_spec\.rb$/, '') - cassette_name = "#{example.example_group.description} #{example.description}".gsub(/[^0-9A-Za-z.\-]+/, '_').downcase - request_profile = case example.metadata[:use_vcr] - when true - %i[method uri] - when :with_oauth_headers - %i[method uri oauth_headers] - end - - VCR.use_cassette(cassette_path.join(cassette_name), match_requests_on: request_profile) do - example.run - end - end -end - module RSpec VCR_ADVISORY = <<~MSG.freeze If this test is failing unexpectedly, the VCR cassette may be to blame. @@ -87,7 +69,27 @@ module RSpec end RSpec.configure do |config| - config.around(:each, use_vcr: true, &VCRHelper.method(:auto_record)) + config.around(:each, use_vcr: true) do |example| + vcr_options = Array(example.metadata[:use_vcr]) + + spec_path = Pathname.new(example.file_path).realpath + cassette_path = spec_path.relative_path_from(Rails.root.join('spec')).sub(/_spec\.rb$/, '') + cassette_name = "#{example.example_group.description} #{example.description}".gsub(/[^0-9A-Za-z.\-]+/, '_').downcase + request_profile = [ + :method, + :uri, + vcr_options.include?(:with_oauth_headers) ? :oauth_headers : nil + ].compact + + VCR.use_cassette(cassette_path.join(cassette_name), match_requests_on: request_profile) do |cassette| + if vcr_options.include?(:time_sensitive) && !cassette.recording? + travel_to(cassette.http_interactions.interactions.first.recorded_at) + end + + example.run + end + end + config.around(:each, use_vcr: true, &RSpec::Support::VCRHelper.method(:inject_advisory)) config.around(:each, use_vcr: true, &RSpec::Expectations::VCRHelper.method(:inject_advisory)) end