Skip to content

Commit 7cfeae1

Browse files
committed
Update VCR logic
1 parent b1f96d7 commit 7cfeae1

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

lib/cypress_on_rails/vcr/base_middleware.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ def vcr
2525
def configure_vcr
2626
require 'vcr'
2727
VCR.configure do |config|
28-
config.cassette_library_dir = "#{configuration.install_folder}/fixtures/vcr_cassettes"
28+
config.cassette_library_dir = cassette_library_dir
2929
end
3030
VCR
3131
end
3232

33+
def cassette_library_dir
34+
"#{configuration.install_folder}/fixtures/vcr_cassettes"
35+
end
36+
3337
def raise_not_implemented
3438
raise NotImplementedError,
3539
'BaseMiddleware can not be initialized directly, use InsertEjectMiddleware or UseCassetteMiddleware'

lib/cypress_on_rails/vcr/use_cassette_middleware.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'cypress_on_rails/configuration'
21
require_relative 'base_middleware'
32

43
module CypressOnRails
@@ -11,6 +10,11 @@ def initialize(app, vcr = nil)
1110
end
1211

1312
def call(env)
13+
vcr_initialized = vcr_defined? &&
14+
VCR.configuration.cassette_library_dir.present? &&
15+
VCR.configuration.cassette_library_dir != cassette_library_dir
16+
return @app.call(env) if vcr_initialized
17+
1418
WebMock.enable! if defined?(WebMock)
1519
vcr.turn_on!
1620
request = Rack::Request.new(env)
@@ -23,6 +27,10 @@ def call(env)
2327

2428
private
2529

30+
def vcr_defined?
31+
defined?(VCR) != nil
32+
end
33+
2634
def fetch_request_cassette(request)
2735
if request.path.start_with?('/graphql') && request.params.key?('operation')
2836
"#{request.path}/#{request.params['operation']}"

spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ def rack_input(json_value)
3939
expect(vcr).to have_received(:use_cassette)
4040
.with('/test/path', hash_including(record: :once))
4141
end
42+
43+
context 'when VCR cassette library directory does not match' do
44+
before do
45+
allow(VCR.configuration).to receive(:cassette_library_dir).and_return('/different/path')
46+
end
47+
48+
it 'returns the application response without using VCR' do
49+
env['PATH_INFO'] = '/test/path'
50+
51+
expect(response).to eq([200, {}, ['app did /test/path']])
52+
expect(vcr).not_to have_received(:use_cassette)
53+
end
54+
end
55+
56+
context 'when VCR is not defined' do
57+
before do
58+
allow(subject).to receive(:vcr_defined?).and_return(false)
59+
end
60+
61+
it 'returns the application response without error' do
62+
env['PATH_INFO'] = '/graphql'
63+
env['QUERY_STRING'] = 'operation=test'
64+
65+
expect(response).to eq([200, {}, ['app did /graphql']])
66+
expect(vcr).to have_received(:use_cassette)
67+
.with('/graphql/test', hash_including(record: :new_episodes))
68+
end
69+
end
4270
end
4371
end
4472
end

0 commit comments

Comments
 (0)