我怎样才能用录像机按要求制作存根呢?
问题是,真实的的请求是在测试中提出的,我想把它存根。
RSpec.describe CreditRegisterLoader do
describe ".call" do
it "should create credit institutions", :vcr do
Timecop.freeze(2020, 3, 25, 13, 0, 0) do
expect { described_class.new.call }.to change { CreditInstitution.count }.by(4965)
end
end
end
end
下面也是我的类CreditRegisterLoader的代码,我想测试它:
class CreditRegisterLoader < ApplicationService
def initialize
@timestamp = (Time.now - 1.minute).to_i.to_s
end
def call
sai_response = get_credit_institutions
unless sai_response
Airbrake.notify("invalid_sai_response")
return
end
begin
CreditInstitutionUpdater.new(JSON.parse(sai_response.body)).create
rescue => error
Airbrake.notify(error)
end
end
private
def get_credit_institutions
RestClient::Request.execute(
method: :post,
url: "https://sai.dpl.europa.eu/register/api/search/entities?t=#{@timestamp}",
headers: {
"Content-Type" => "application/json",
"Accept" => "application/json",
},
payload: JSON.generate({"$and": [{"_messagetype": "SAIDPL"}]})
)
end
end
1条答案
按热度按时间dddzy1tm1#
我建议采用以下解决方案