在rspec测试中使用VCR,一切正常。但我想提高代码质量,因为我在测试中多次使用相同的代码VCR.use_cassette
。
以下是测试:-
context 'when request authenticated and Params are valid' do
it 'results should have success' do
VCR.use_cassette('test_service') do
post '/rates', headers: auth_headers, params: params
expect(response.status).to be(200)
end
end
it 'results should have nominal nominal_interest_rate' do
VCR.use_cassette('test_service') do
post '/rates', headers: auth_headers, params: params
expect(response.body).to include('rate')
expect(JSON.parse(response.body).class).to eq(Hash)
end
end
it 'results should have nominal discount' do
VCR.use_cassette('test_service') do
post '/rates', headers: auth_headers, params: params
expect(response.body).to include('discount')
end
end
end
在上面的代码中,我已经多次使用VCR.use_cassette。我想把它作为一个公共块,并在需要的时候多次使用。我该怎么做?
根据您可以使用的单据"RSpec元数据";并将盒式磁带作为自变量({vcr: 'cassette name'}
(传递给示例或示例组,例如
"spec/spec_helper.rb";
VCR.configure do |c|
c.configure_rspec_metadata!
end
然后
"spec/your_spec.rb">
context 'when request authenticated and Params are valid', vcr: 'test_service' do
before(:each) do
post '/rates', headers: auth_headers, params: params
end
it 'results should have success' do
expect(response.status).to be(200)
end
it 'results should have nominal nominal_interest_rate' do
expect(response.body).to include('rate')
expect(JSON.parse(response.body).class).to eq(Hash)
end
it 'results should have nominal discount' do
expect(response.body).to include('discount')
end
end