Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/cypress_on_rails/vcr_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class VCRMiddleware
def initialize(app, vcr = nil)
@app = app
@vcr = vcr
@first_call = false
end

def call(env)
Expand All @@ -19,13 +20,16 @@ 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

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]
Expand All @@ -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]]
Expand All @@ -56,5 +61,13 @@ def vcr
end
@vcr = VCR
end

def do_first_call
@first_call = true
vcr.turn_off!
WebMock.disable! if defined?(WebMock)
rescue LoadError
# nop
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
})
12 changes: 11 additions & 1 deletion spec/cypress_on_rails/vcr_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) { {} }
Expand All @@ -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
Expand Down Expand Up @@ -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|
Expand All @@ -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
Expand Down