From aa053cbf5b6867c6a70f834e82ea945975e95da1 Mon Sep 17 00:00:00 2001 From: Grant Petersen-Speelman Date: Thu, 10 Mar 2022 14:09:14 +1100 Subject: [PATCH 1/3] Turn VCR on and off between inserts and ejects --- README.md | 6 +++++- lib/cypress_on_rails/vcr_middleware.rb | 11 +++++++++++ .../templates/spec/cypress/app_commands/clean.rb | 4 +++- spec/cypress_on_rails/vcr_middleware_spec.rb | 12 +++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e269c80..f5de8a6 100644 --- a/README.md +++ b/README.md @@ -283,9 +283,11 @@ yarn add cypress-on-rails --dev ### for VCR +This only works when you start the rails server with a single worker and single thread + #### setup -Add you VCR configuration to your `cypress_helper.rb` +Add your VCR configuration to your `cypress_helper.rb` ```ruby require 'vcr' @@ -304,6 +306,8 @@ Add to you `clean.rb` ```ruby VCR.eject_cassette # make sure we no cassettes inserted before the next test starts +VCR.turn_off! +WebMock.disable! if defined?(WebMock) ``` #### usage diff --git a/lib/cypress_on_rails/vcr_middleware.rb b/lib/cypress_on_rails/vcr_middleware.rb index 71dd3f7..1aed6fe 100644 --- a/lib/cypress_on_rails/vcr_middleware.rb +++ b/lib/cypress_on_rails/vcr_middleware.rb @@ -10,6 +10,7 @@ class VCRMiddleware def initialize(app, vcr = nil) @app = app @vcr = vcr + @first_call = false end def call(env) @@ -19,6 +20,7 @@ def call(env) elsif request.path.start_with?('/__cypress__/vcr/eject') configuration.tagged_logged { handle_eject } else + do_first_call unless @first_call @app.call(env) end end @@ -26,6 +28,8 @@ def call(env) private def handle_insert(req) + WebMock.enable! if defined?(WebMock) + vcr.turn_on! body = JSON.parse(req.body.read) logger.info "vcr insert cassette: #{body}" cassette_name = body[0] @@ -43,6 +47,7 @@ def handle_insert(req) def handle_eject logger.info "vcr eject cassette" vcr.eject_cassette + do_first_call [201, {'Content-Type' => 'application/json'}, [{'message': 'OK'}.to_json]] rescue LoadError, ArgumentError => e [501, {'Content-Type' => 'application/json'}, [{'message': e.message}.to_json]] @@ -56,5 +61,11 @@ def vcr end @vcr = VCR end + + def do_first_call + @first_call = true + vcr.turn_off! + WebMock.disable! if defined?(WebMock) + end end end diff --git a/lib/generators/cypress_on_rails/templates/spec/cypress/app_commands/clean.rb b/lib/generators/cypress_on_rails/templates/spec/cypress/app_commands/clean.rb index 85bcf9c..10c3a82 100644 --- a/lib/generators/cypress_on_rails/templates/spec/cypress/app_commands/clean.rb +++ b/lib/generators/cypress_on_rails/templates/spec/cypress/app_commands/clean.rb @@ -10,7 +10,9 @@ CypressOnRails::SmartFactoryWrapper.reload if defined?(VCR) - VCR.eject_cassette # make sure we any cassettes inserted before the next test starts + VCR.eject_cassette # make sure we no cassette inserted before the next test starts + VCR.turn_off! + WebMock.disable! if defined?(WebMock) end Rails.logger.info "APPCLEANED" # used by log_fail.rb diff --git a/spec/cypress_on_rails/vcr_middleware_spec.rb b/spec/cypress_on_rails/vcr_middleware_spec.rb index 5c7f5c9..34906aa 100644 --- a/spec/cypress_on_rails/vcr_middleware_spec.rb +++ b/spec/cypress_on_rails/vcr_middleware_spec.rb @@ -5,7 +5,7 @@ module CypressOnRails RSpec.describe VCRMiddleware do let(:app) { ->(env) { [200, {}, ["app did #{env['PATH_INFO']}"]] } } - let(:vcr) { class_double(VCR, insert_cassette: true, eject_cassette: true) } + let(:vcr) { class_double(VCR, turn_on!: true, turn_off!: true, insert_cassette: true, eject_cassette: true) } subject { described_class.new(app, vcr) } let(:env) { {} } @@ -28,6 +28,7 @@ def rack_input(json_value) expect(response).to eq([201, {"Content-Type"=>"application/json"}, ["{\"message\":\"OK\"}"]]) + expect(vcr).to have_received(:turn_on!) expect(vcr).to have_received(:insert_cassette).with('cas1', {}) end end @@ -87,12 +88,20 @@ def rack_input(json_value) expect(response).to eq([201, {"Content-Type"=>"application/json"}, ["{\"message\":\"OK\"}"]]) + expect(vcr).to have_received(:turn_off!) expect(vcr).to have_received(:eject_cassette) end end end describe '"Other paths"' do + it 'calls vcr turn off the first time' do + env['PATH_INFO'] = '/test' + + expect(response).to eq([200, {}, ["app did /test"]]) + expect(vcr).to have_received(:turn_off!) + end + it 'runs app' do aggregate_failures do %w(/ /__cypress__/login command /cypress_command /).each do |path| @@ -101,6 +110,7 @@ def rack_input(json_value) response = subject.call(env) expect(response).to eq([200, {}, ["app did #{path}"]]) + expect(vcr).to have_received(:turn_off!) end end end From 622557f067f09cc9b6a1248758068b346464a9ac Mon Sep 17 00:00:00 2001 From: Grant Petersen-Speelman Date: Thu, 10 Mar 2022 14:12:31 +1100 Subject: [PATCH 2/3] fix load error --- lib/cypress_on_rails/vcr_middleware.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/cypress_on_rails/vcr_middleware.rb b/lib/cypress_on_rails/vcr_middleware.rb index 1aed6fe..6fe9a43 100644 --- a/lib/cypress_on_rails/vcr_middleware.rb +++ b/lib/cypress_on_rails/vcr_middleware.rb @@ -66,6 +66,8 @@ def do_first_call @first_call = true vcr.turn_off! WebMock.disable! if defined?(WebMock) + rescue LoadError + # nop end end end From 7b3c546f37321ef0cf2ba67a5ffebd2edd683f7c Mon Sep 17 00:00:00 2001 From: Grant Petersen-Speelman Date: Thu, 10 Mar 2022 14:33:47 +1100 Subject: [PATCH 3/3] update spec --- .../integration/rails_examples/using_vcr_spec.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/generators/cypress_on_rails/templates/spec/cypress/integration/rails_examples/using_vcr_spec.js b/lib/generators/cypress_on_rails/templates/spec/cypress/integration/rails_examples/using_vcr_spec.js index 7751d20..0cb9a47 100644 --- a/lib/generators/cypress_on_rails/templates/spec/cypress/integration/rails_examples/using_vcr_spec.js +++ b/lib/generators/cypress_on_rails/templates/spec/cypress/integration/rails_examples/using_vcr_spec.js @@ -6,12 +6,19 @@ describe('Rails Other examples', function() { cy.visit('/using_vcr/index') cy.get('a').contains('Cats').click() - cy.contains('Wikipedia has a recording of a cat meowing, because why not?') + cy.contains('Record from Cats API'); cy.vcr_eject_cassette(); + }) + + it('Using previous a cassette', function() { + cy.app('clean') // have a look at cypress/app_commands/clean.rb cy.vcr_insert_cassette('cats') - cy.visit('/using_vcr/record_cats') - cy.contains('Wikipedia has a recording of a cat meowing, because why not?') + cy.visit('/using_vcr/index') + cy.get('a').contains('Cats').click() + cy.contains('Record from Cats API'); + + cy.vcr_eject_cassette(); }) })