我正在尝试测试使用Faraday和VCR的解析器。测试第一次通过并创建一个空盒子。当我再次运行时,它击中空盒子并失败。我在控制台中测试了解析器,每次都可以工作。这是代码:
配置
require 'vcr'
# require 'webmock/rspec'
RSpec.configure do |config|
VCR.configure do |c|
c.cassette_library_dir = 'spec/vcr'
c.default_cassette_options = { record: :new_episodes}
c.hook_into :webmock
end
Parser Class
require 'zip
require 'net/http'
class Parser
attr_reader :response
def initialize
@response = Faraday.get("http://www.accessdata.fda.gov/premarket/ftparea/public.zip")
end
def unzipped_file
Zip::InputStream.open(StringIO.new(response.body)) do |io|
while (entry = io.get_next_entry)
lines = io.read.split(/\r\n/)
end
lines
end
end
def headers
%w(name address1 address2 address3 city state zip phone fax)
end
def split_lines
unzipped_file.map { |line| line.split("|") }
end
def records
split_lines.map { |line| headers.zip(line).to_h }
end
end
规范文件
describe Parser do
describe 'api call' do
it 'gets a response from api' do
VCR.use_cassette('parser') do
response = Parser.new.records
expect(response.first.class).to eq Hash
end
端 结束 端