How to use linked method of VCR Package

Best Vcr_ruby code snippet using VCR.linked

vcr.rb

Source:vcr.rb Github

copy

Full Screen

2require 'vcr/util/variable_args_block_caller'3require 'vcr/cassette'4require 'vcr/cassette/serializers'5require 'vcr/cassette/persisters'6require 'vcr/linked_cassette'7require 'vcr/configuration'8require 'vcr/deprecations'9require 'vcr/errors'10require 'vcr/library_hooks'11require 'vcr/request_ignorer'12require 'vcr/request_matcher_registry'13require 'vcr/structs'14require 'vcr/version'15# The main entry point for VCR.16# @note This module is extended onto itself; thus, the methods listed17# here as instance methods are available directly off of VCR.18module VCR19 include VariableArgsBlockCaller20 include Errors21 extend self22 # Mutex to synchronize access to cassettes in a threaded environment23 CassetteMutex = Mutex.new24 # The main thread in which VCR was loaded25 MainThread = Thread.current26 autoload :CucumberTags, 'vcr/test_frameworks/cucumber'27 autoload :InternetConnection, 'vcr/util/internet_connection'28 module RSpec29 autoload :Metadata, 'vcr/test_frameworks/rspec'30 end31 module Middleware32 autoload :Faraday, 'vcr/middleware/faraday'33 autoload :Rack, 'vcr/middleware/rack'34 end35 # The currently active cassette.36 #37 # @return [nil, VCR::Cassette] The current cassette or nil if there is38 # no current cassette.39 def current_cassette40 cassettes.last41 end42 # Inserts the named cassette using the given cassette options.43 # New HTTP interactions, if allowed by the cassette's `:record` option, will44 # be recorded to the cassette. The cassette's existing HTTP interactions45 # will be used to stub requests, unless prevented by the cassete's46 # `:record` option.47 #48 # @example49 # VCR.insert_cassette('twitter', :record => :new_episodes)50 #51 # # ...later, after making an HTTP request:52 #53 # VCR.eject_cassette54 #55 # @param name [#to_s] The name of the cassette. VCR will sanitize56 # this to ensure it is a valid file name.57 # @param options [Hash] The cassette options. The given options will58 # be merged with the configured default_cassette_options.59 # @option options :record [:all, :none, :new_episodes, :once] The record mode.60 # @option options :erb [Boolean, Hash] Whether or not to evaluate the61 # cassette as an ERB template. Defaults to false. A hash can be used62 # to provide the ERB template with local variables.63 # @option options :match_requests_on [Array<Symbol, #call>] List of request matchers64 # to use to determine what recorded HTTP interaction to replay. Defaults to65 # [:method, :uri]. The built-in matchers are :method, :uri, :host, :path, :headers66 # and :body. You can also pass the name of a registered custom request matcher or67 # any object that responds to #call.68 # @option options :re_record_interval [Integer] When given, the69 # cassette will be re-recorded at the given interval, in seconds.70 # @option options :tag [Symbol] Used to apply tagged `before_record`71 # and `before_playback` hooks to the cassette.72 # @option options :tags [Array<Symbol>] Used to apply multiple tags to73 # a cassette so that tagged `before_record` and `before_playback` hooks74 # will apply to the cassette.75 # @option options :update_content_length_header [Boolean] Whether or76 # not to overwrite the Content-Length header of the responses to77 # match the length of the response body. Defaults to false.78 # @option options :decode_compressed_response [Boolean] Whether or79 # not to decode compressed responses before recording the cassette.80 # This makes the cassette more human readable. Defaults to false.81 # @option options :allow_playback_repeats [Boolean] Whether or not to82 # allow a single HTTP interaction to be played back multiple times.83 # Defaults to false.84 # @option options :allow_unused_http_interactions [Boolean] If set to85 # false, an error will be raised if a cassette is ejected before all86 # previously recorded HTTP interactions have been used.87 # Defaults to true. Note that when an error has already occurred88 # (as indicated by the `$!` variable) unused interactions will be89 # allowed so that we don't silence the original error (which is almost90 # certainly more interesting/important).91 # @option options :exclusive [Boolean] Whether or not to use only this92 # cassette and to completely ignore any cassettes in the cassettes stack.93 # Defaults to false.94 # @option options :serialize_with [Symbol] Which serializer to use.95 # Valid values are :yaml, :syck, :psych, :json or any registered96 # custom serializer. Defaults to :yaml.97 # @option options :persist_with [Symbol] Which cassette persister to98 # use. Defaults to :file_system. You can also register and use a99 # custom persister.100 # @option options :persister_options [Hash] Pass options to the101 # persister specified in `persist_with`. Currently available options for the file_system persister:102 # - `:downcase_cassette_names`: when `true`, names of cassettes will be103 # normalized in lowercase before reading and writing, which can avoid104 # confusion when using both case-sensitive and case-insensitive file105 # systems.106 # @option options :preserve_exact_body_bytes [Boolean] Whether or not107 # to base64 encode the bytes of the requests and responses for this cassette108 # when serializing it. See also `VCR::Configuration#preserve_exact_body_bytes`.109 #110 # @return [VCR::Cassette] the inserted cassette111 #112 # @raise [ArgumentError] when the given cassette is already being used.113 # @raise [VCR::Errors::TurnedOffError] when VCR has been turned off114 # without using the :ignore_cassettes option.115 # @raise [VCR::Errors::MissingERBVariableError] when the `:erb` option116 # is used and the ERB template requires variables that you did not provide.117 #118 # @note If you use this method you _must_ call `eject_cassette` when you119 # are done. It is generally recommended that you use {#use_cassette}120 # unless your code-under-test cannot be run as a block.121 #122 def insert_cassette(name, options = {})123 if turned_on?124 if cassettes.any? { |c| c.name == name }125 raise ArgumentError.new("There is already a cassette with the same name (#{name}). You cannot nest multiple cassettes with the same name.")126 end127 cassette = Cassette.new(name, options)128 context_cassettes.push(cassette)129 cassette130 elsif !ignore_cassettes?131 message = "VCR is turned off. You must turn it on before you can insert a cassette. " +132 "Or you can use the `:ignore_cassettes => true` option to completely ignore cassette insertions."133 raise TurnedOffError.new(message)134 end135 end136 # Ejects the current cassette. The cassette will no longer be used.137 # In addition, any newly recorded HTTP interactions will be written to138 # disk.139 #140 # @param options [Hash] Eject options.141 # @option options :skip_no_unused_interactions_assertion [Boolean]142 # If `true` is given, this will skip the "no unused HTTP interactions"143 # assertion enabled by the `:allow_unused_http_interactions => false`144 # cassette option. This is intended for use when your test has had145 # an error, but your test framework has already handled it.146 # @return [VCR::Cassette, nil] the ejected cassette if there was one147 def eject_cassette(options = {})148 cassette = cassettes.last149 cassette.eject(options) if cassette150 cassette151 ensure152 context_cassettes.delete(cassette)153 end154 # Inserts a cassette using the given name and options, runs the given155 # block, and ejects the cassette.156 #157 # @example158 # VCR.use_cassette('twitter', :record => :new_episodes) do159 # # make an HTTP request160 # end161 #162 # @param (see #insert_cassette)163 # @option (see #insert_cassette)164 # @yield Block to run while this cassette is in use.165 # @yieldparam cassette [(optional) VCR::Cassette] the cassette that has166 # been inserted.167 # @raise (see #insert_cassette)168 # @return [void]169 # @see #insert_cassette170 # @see #eject_cassette171 def use_cassette(name, options = {}, &block)172 unless block173 raise ArgumentError, "`VCR.use_cassette` requires a block. " +174 "If you cannot wrap your code in a block, use " +175 "`VCR.insert_cassette` / `VCR.eject_cassette` instead."176 end177 cassette = insert_cassette(name, options)178 begin179 call_block(block, cassette)180 rescue StandardError181 cassette.run_failed!182 raise183 ensure184 eject_cassette185 end186 end187 # Inserts multiple cassettes the given names188 #189 # @example190 # cassettes = [191 # { name: 'github' },192 # { name: 'apple', options: { erb: true } }193 # ]194 # VCR.use_cassettes(cassettes) do195 # # make multiple HTTP requests196 # end197 def use_cassettes(cassettes, &block)198 cassette = cassettes.pop199 use_cassette(cassette[:name], cassette[:options] || {}) do200 if cassettes.empty?201 block.call202 else203 use_cassettes(cassettes, &block)204 end205 end206 end207 # Used to configure VCR.208 #209 # @example210 # VCR.configure do |c|211 # c.some_config_option = true212 # end213 #214 # @yield the configuration block215 # @yieldparam config [VCR::Configuration] the configuration object216 # @return [void]217 def configure218 yield configuration219 end220 # @return [VCR::Configuration] the VCR configuration.221 def configuration222 @configuration223 end224 # Sets up `Before` and `After` cucumber hooks in order to225 # use VCR with particular cucumber tags.226 #227 # @example228 # VCR.cucumber_tags do |t|229 # t.tags "tag1", "tag2"230 # t.tag "@some_other_tag", :record => :new_episodes231 # end232 #233 # @yield the cucumber tags configuration block234 # @yieldparam t [VCR::CucumberTags] Cucumber tags config object235 # @return [void]236 # @see VCR::CucumberTags#tags237 def cucumber_tags(&block)238 main_object = eval('self', block.binding)239 yield VCR::CucumberTags.new(main_object)240 end241 # Turns VCR off for the duration of a block.242 #243 # @param (see #turn_off!)244 # @return [void]245 # @raise (see #turn_off!)246 # @see #turn_off!247 # @see #turn_on!248 # @see #turned_on?249 def turned_off(options = {})250 turn_off!(options)251 begin252 yield253 ensure254 turn_on!255 end256 end257 # Turns VCR off, so that it no longer handles every HTTP request.258 #259 # @param options [Hash] hash of options260 # @option options :ignore_cassettes [Boolean] controls what happens when a cassette is261 # inserted while VCR is turned off. If `true` is passed, the cassette insertion262 # will be ignored; otherwise a {VCR::Errors::TurnedOffError} will be raised.263 #264 # @return [void]265 # @raise [VCR::Errors::CassetteInUseError] if there is currently a cassette in use266 # @raise [ArgumentError] if you pass an invalid option267 def turn_off!(options = {})268 if VCR.current_cassette269 raise CassetteInUseError, "A VCR cassette is currently in use (#{VCR.current_cassette.name}). " +270 "You must eject it before you can turn VCR off."271 end272 set_context_value(:ignore_cassettes, options.fetch(:ignore_cassettes, false))273 invalid_options = options.keys - [:ignore_cassettes]274 if invalid_options.any?275 raise ArgumentError.new("You passed some invalid options: #{invalid_options.inspect}")276 end277 set_context_value(:turned_off, true)278 end279 # Turns on VCR, if it has previously been turned off.280 # @return [void]281 # @see #turn_off!282 # @see #turned_off283 # @see #turned_on?284 def turn_on!285 set_context_value(:turned_off, false)286 end287 # @return whether or not VCR is turned on288 # @note Normally VCR is _always_ turned on; it will only be off if you have289 # explicitly turned it off.290 # @see #turn_on!291 # @see #turn_off!292 # @see #turned_off293 def turned_on?294 linked_context = current_context[:linked_context]295 return !linked_context[:turned_off] if linked_context296 !context_value(:turned_off)297 end298 # @private299 def http_interactions300 return current_cassette.http_interactions if current_cassette301 VCR::Cassette::HTTPInteractionList::NullList302 end303 # @private304 def real_http_connections_allowed?305 return current_cassette.recording? if current_cassette306 !!(configuration.allow_http_connections_when_no_cassette? || !turned_on?)307 end308 # @return [RequestMatcherRegistry] the request matcher registry309 def request_matchers310 @request_matchers311 end312 # @return [Enumerable] list of all cassettes currently being used313 def cassettes(context = current_context)314 linked_context = context[:linked_context]315 linked_cassettes = cassettes(linked_context) if linked_context316 LinkedCassette.list(context[:cassettes], Array(linked_cassettes))317 end318 # @private319 def request_ignorer320 @request_ignorer321 end322 # @private323 def library_hooks324 @library_hooks325 end326 # @private327 def cassette_serializers328 @cassette_serializers329 end330 # @private331 def cassette_persisters332 @cassette_persisters333 end334 # @private335 def record_http_interaction(interaction)336 return unless cassette = current_cassette337 return if VCR.request_ignorer.ignore?(interaction.request)338 cassette.record_http_interaction(interaction)339 end340 # @private341 def link_context(from_thread, to_key)342 @context[to_key] = get_context(from_thread)343 end344 # @private345 def unlink_context(key)346 @context.delete(key)347 end348 # @private349 def fibers_available?350 @fibers_available351 end352private353 def current_context354 get_context(Thread.current, Fiber.current)355 end356 def get_context(thread_key, fiber_key = nil)357 context = @context[fiber_key] if fiber_key358 context ||= @context[thread_key]359 if context360 context361 else362 @context[thread_key] = dup_context(@context[MainThread])363 end364 end365 def context_value(name)366 current_context[name]367 end368 def set_context_value(name, value)369 current_context[name] = value370 end371 def dup_context(context)372 {373 :turned_off => context[:turned_off],374 :ignore_cassettes => context[:ignore_cassettes],375 :cassettes => [],376 :linked_context => context377 }378 end379 def ignore_cassettes?380 context_value(:ignore_cassettes)381 end382 def context_cassettes383 context_value(:cassettes)384 end385 def initialize_fibers386 begin387 require 'fiber'388 @fibers_available = true389 rescue LoadError390 @fibers_available = false391 end392 end393 def initialize_ivars394 initialize_fibers395 @context = {396 MainThread => {397 :turned_off => false,398 :ignore_cassettes => false,399 :cassettes => [],400 :linked_context => nil401 }402 }403 @configuration = Configuration.new404 @request_matchers = RequestMatcherRegistry.new405 @request_ignorer = RequestIgnorer.new406 @library_hooks = LibraryHooks.new407 @cassette_serializers = Cassette::Serializers.new408 @cassette_persisters = Cassette::Persisters.new409 end410 initialize_ivars # to avoid warnings411end...

Full Screen

Full Screen

search_spec.rb

Source:search_spec.rb Github

copy

Full Screen

...157 first_person.first_name.should == 'Charles'158 first_person.last_name.should == 'Garcia, CFA'159 first_person.id.should == '2zk34r8TvA'160 first_person.picture_url.should be_nil161 first_person.public_profile_url.should == 'http://www.linkedin.com/in/charlesgarcia'162 end163 end164 describe "by company_name option", vcr: vcr_options do165 let(:results) do166 client.search(:company_name => 'IBM')167 end168 it "should perform a search" do169 results.people.all.size.should == 6170 results.people.all.first.first_name.should == 'Ryan'171 results.people.all.first.last_name.should == 'Sue'172 results.people.all.first.id.should == 'KHkgwBMaa-'173 end174 end175 describe "#field_selector" do...

Full Screen

Full Screen

linked

Using AI Code Generation

copy

Full Screen

1 c.default_cassette_options = { :record => :new_episodes }2 c.default_cassette_options = { :record => :new_episodes }3 c.default_cassette_options = { :record => :new_episodes }4 c.default_cassette_options = { :record => :new_episodes }5 c.default_cassette_options = { :record => :new_episodes }6 c.default_cassette_options = { :record => :new_episodes }

Full Screen

Full Screen

linked

Using AI Code Generation

copy

Full Screen

1VCR.use_cassette("test") do2VCR.use_cassette("test") do3VCR.use_cassette("test") do4VCR.use_cassette("test") do5VCR.use_cassette("test") do6VCR.use_cassette("test") do7VCR.use_cassette("test") do8VCR.use_cassette("test") do9VCR.use_cassette("test") do10VCR.use_cassette("test") do11VCR.use_cassette("test") do12VCR.use_cassette("test") do13VCR.use_cassette("test") do14VCR.use_cassette("test") do

Full Screen

Full Screen

linked

Using AI Code Generation

copy

Full Screen

1VCR.use_cassette('test') do2VCR.use_cassette('test') do3VCR.use_cassette('test') do4VCR.use_cassette('test') do5VCR.use_cassette('test') do6VCR.use_cassette('test') do7VCR.use_cassette('test') do8VCR.use_cassette('test') do9VCR.use_cassette('test') do10VCR.use_cassette('test') do11VCR.use_cassette('test') do12VCR.use_cassette('test') do13VCR.use_cassette('test') do14VCR.use_cassette('test') do

Full Screen

Full Screen

linked

Using AI Code Generation

copy

Full Screen

1 def self.use_cassette(name, options = {})2 def self.use_cassette(name, options = {})3 def self.use_linked_cassette(name, options = {})4 def self.use_cassette(name, options = {})5 def self.use_linked_cassette(name, options = {})6 def self.use_linked_cassette(name, options = {})7 def self.use_cassette(name, options = {})8 def self.use_linked_cassette(name, options = {})9 def self.use_linked_cassette(name, options = {})10 def self.use_linked_cassette(name, options = {})

Full Screen

Full Screen

linked

Using AI Code Generation

copy

Full Screen

1 c.default_cassette_options = { :record => :new_episodes }2 c.default_cassette_options = { :record => :new_episodes }3 c.default_cassette_options = { :record => :new_episodes }4 c.default_cassette_options = { :record => :new_episodes }5 c.default_cassette_options = { :record => :new_episodes }6 c.default_cassette_options = { :record => :new_episodes }

Full Screen

Full Screen

linked

Using AI Code Generation

copy

Full Screen

1 def self.use_cassette(name, options = {})2 def self.use_cassette(name, options = {})3 def self.use_linked_cassette(name, options = {})4 def self.use_cassette(name, options = {})5 def self.use_linked_cassette(name, options = {})6 def self.use_linked_cassette(name, options = {})7 def self.use_cassette(name, options = {})8 def self.use_linked_cassette(name, options = {})9 def self.use_linked_cassette(name, options = {})10 def self.use_linked_cassette(name, options = {})

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Vcr_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful