Best Vcr_ruby code snippet using VCR.storage_location
persister_spec.rb
Source: persister_spec.rb
...3 module Gzip4 describe Persister do5 describe "#[]" do6 it 'gunzips and reads from the given file' do7 file_path = Persister.storage_location + '/foo.txt.gz'8 Zlib::GzipWriter.open(file_path) do |gz|9 gz.write('12345')10 end11 Persister["foo.txt"].should eq("12345")12 end13 it 'handles directories in the given file name' do14 FileUtils.mkdir_p Persister.storage_location + '/a'15 Zlib::GzipWriter.open(Persister.storage_location + '/a/b.gz') do |gz|16 gz.write('1234')17 end18 Persister["a/b"].should eq("1234")19 end20 it 'returns nil if the file does not exist' do21 Persister["non_existant_file"].should be_nil22 end23 end24 describe "#[]=" do25 it 'gzips and writes the given file contents to the given file name' do26 file_path = Persister.storage_location + '/foo.txt.gz'27 File.exist?(file_path).should be_false28 Persister["foo.txt"] = "bar"29 Zlib::GzipReader.open(file_path) { |gz| gz.read.should eq("bar") }30 end31 it 'creates any needed intermediary directories' do32 File.exist?(Persister.storage_location + '/a').should be_false33 Persister["a/b"] = "bar"34 file_path = Persister.storage_location + '/a/b.gz'35 Zlib::GzipReader.open(file_path) { |gz| gz.read.should eq("bar") }36 end37 end38 describe "#storage_location" do39 before do40 @previous_location = Persister.storage_location41 @previous_default_location = VCR.configuration.cassette_library_dir42 end43 after do44 Persister.storage_location = @previous_location45 VCR.configuration.cassette_library_dir = @previous_default_location46 end47 it "returns VCR.configuration.cassette_library_dir by default" do48 default_location = @previous_default_location + '/default_location'49 VCR::Cassette::Persisters::FileSystem.storage_location = default_location50 Persister.storage_location.should == default_location51 end52 it "allows overwriting the default location" do53 location = @previous_default_location + '/overwritten_location'54 Persister.storage_location = location55 Persister.storage_location.should == location56 end57 it "raises an exception if no location is defined and default is not present" do58 VCR::Cassette::Persisters::FileSystem.storage_location = nil59 Persister.storage_location = nil60 expect {61 Persister.storage_location62 }.to raise_error(/Vcr::Gzip::Persister.storage_location is missing./)63 end64 end65 describe "#absolute_path_to_file" do66 it "returns the absolute path to file relative to the storage location with .gz extension" do67 expected = File.join(Persister.storage_location, "bar/bazz.json.gz")68 Persister.absolute_path_to_file("bar/bazz.json").should eq(expected)69 end70 it "sanitizes the file name" do71 expected = File.join(Persister.storage_location, "_t_i-t_1_2_f_n.json.gz")72 Persister.absolute_path_to_file("\nt \t! i-t_1.2_f n.json").should eq(expected)73 expected = File.join(Persister.storage_location, "a_1/b.gz")74 Persister.absolute_path_to_file("a 1/b").should eq(expected)75 end76 end77 end78 end79end...
file_system_spec.rb
Source: file_system_spec.rb
...3module VCR4 class Cassette5 class Persisters6 describe FileSystem do7 before { FileSystem.storage_location = VCR.configuration.cassette_library_dir }8 describe "#[]" do9 it 'reads from the given file, relative to the configured storage location' do10 File.open(FileSystem.storage_location + '/foo.txt', 'w') { |f| f.write('1234') }11 expect(FileSystem["foo.txt"]).to eq("1234")12 end13 it 'handles directories in the given file name' do14 FileUtils.mkdir_p FileSystem.storage_location + '/a'15 File.open(FileSystem.storage_location + '/a/b', 'w') { |f| f.write('1234') }16 expect(FileSystem["a/b"]).to eq("1234")17 end18 it 'returns nil if the file does not exist' do19 expect(FileSystem["non_existant_file"]).to be_nil20 end21 end22 describe "#[]=" do23 context 'with a simple file_name and binary content' do24 let(:file_name) { 'foo.txt' }25 let(:content) { SecureRandom.random_bytes(20) }26 let(:location) { FileSystem.storage_location + '/' + file_name }27 it 'writes the given file contents to the given file name' do28 expect(File.exist?(location)).to be false29 FileSystem[file_name] = content30 expect(File.binread(location)).to eq(content)31 end32 end33 it 'creates any needed intermediary directories' do34 expect(File.exist?(FileSystem.storage_location + '/a')).to be false35 FileSystem["a/b"] = "bar"36 expect(File.read(FileSystem.storage_location + '/a/b')).to eq("bar")37 end38 end39 describe "#absolute_path_to_file" do40 it "returns the absolute path to the given relative file based on the storage location" do41 expected = File.join(FileSystem.storage_location, "bar/bazz.json")42 expect(FileSystem.absolute_path_to_file("bar/bazz.json")).to eq(expected)43 end44 it "returns nil if the storage_location is not set" do45 FileSystem.storage_location = nil46 expect(FileSystem.absolute_path_to_file("bar/bazz.json")).to be_nil47 end48 it "sanitizes the file name" do49 expected = File.join(FileSystem.storage_location, "_t_i-t_1_2_f_n.json")50 expect(FileSystem.absolute_path_to_file("\nt \t! i-t_1.2_f n.json")).to eq(expected)51 expected = File.join(FileSystem.storage_location, "a_1/b")52 expect(FileSystem.absolute_path_to_file("a 1/b")).to eq(expected)53 end54 it 'handles files with no extensions (even when there is a dot in the path)' do55 expected = File.join(FileSystem.storage_location, "/foo_bar/baz_qux")56 expect(FileSystem.absolute_path_to_file("/foo.bar/baz qux")).to eq(expected)57 end58 end59 end60 end61 end62end...
Check out the latest blogs from LambdaTest on this topic:
In today’s world, an organization’s most valuable resource is its customers. However, acquiring new customers in an increasingly competitive marketplace can be challenging while maintaining a strong bond with existing clients. Implementing a customer relationship management (CRM) system will allow your organization to keep track of important customer information. This will enable you to market your services and products to these customers better.
Hey LambdaTesters! We’ve got something special for you this week. ????
As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.
The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.
Entering the world of testers, one question started to formulate in my mind: “what is the reason that bugs happen?”.
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!