Last active
August 3, 2016 16:10
-
-
Save qd3v/cbe045cafca5137a0d6c297138bba862 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SpecHelpers | |
# See more on this: https://github.com/vcr/vcr/issues/600 | |
module VCRHooks | |
# @return [Proc] registered proc | |
def register_vcr_hook(type, *args, &block) | |
check_hook_type_valid!(type) | |
# Do not use #hooks hash directly, VCR wrap block into FilteredHook struct | |
VCR.configuration.send(type, *args, &block); block | |
end | |
# NOTE: if you use same block for several hooks they will be removed altogether | |
def unregister_vcr_hook(type, hook) | |
check_hook_type_valid!(type) | |
found = false | |
VCR.configuration.hooks[type].delete_if do |filtered_hook| | |
filtered_hook.hook == hook && found = true | |
end | |
raise "Can't find VCR hook for #{type} to remove" unless found | |
end | |
private | |
VCR_HOOKS = %i[before_http_request after_http_request around_http_request before_playback before_record] | |
def check_hook_type_valid!(type) | |
raise "Unknown VCR hook type: #{type}" unless VCR_HOOKS.include?(type) | |
end | |
end | |
end | |
RSpec.configuration.include(SpecHelpers::VCRHooks, type: :request) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
describe "Creating Braintree customer" do | |
let(:customer_id) { 'dj_app_customer' } | |
let(:customer_data) do | |
# Specify your own customer ID. Customer IDs are case insensitive. | |
{id: customer_id, | |
first_name: 'John', | |
last_name: 'Doe'} | |
end | |
context "new customer" do | |
example "successfully created" do | |
VCR.use_cassette('braintree/customer_creation_success') do | |
result = Braintree::Customer.create(customer_data) | |
expect(result).to be_success | |
end | |
end | |
end | |
context "customer already exists" do | |
let!(:create_customer_hook) do | |
register_vcr_hook(:before_http_request, :real?) do | |
logger.info 'Creating customer before running spec...' | |
# We can't use ``VCR#turn_off!`` here, we have cassette already loaded. | |
# Also, ``WebMock.allow/disable_net_connect!`` cause errors using enable/disable | |
WebMock.disable! | |
begin | |
logger.info "Checking if Customer with ID #{customer_id} already exists..." | |
Braintree::Customer.find(customer_id) | |
rescue Braintree::NotFoundError | |
logger.info 'No Customer exists, running spec...' | |
ensure | |
WebMock.enable! | |
end | |
end | |
end | |
after do | |
unregister_vcr_hook(:before_http_request, create_customer_hook) | |
end | |
it "fail" do | |
VCR.use_cassette('braintree/customer_creation_already_exists', record: :all) do | |
result = Braintree::Customer.create(customer_data) | |
expect(result).not_to be_success | |
end | |
end | |
it "just delete customer for testing hooks purposes" do | |
VCR.turn_off! do | |
Braintree::Customer.delete(customer_id) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment