0

I'm writing a client in ruby for an API that we're using. We use rspec and VCR with webmock to mock request/responses to the API.

What is the best or appropriate way to test the response back from the API when the response payload is really large?

Is there a best practice around where to put the large expected payload and how to test this?

require 'spec_helper'

describe Service::API, vcr: true do
  describe '.method' do
    it 'returns valid response' do
      #returns large body payload
      response = subject.method
      expect(response).to eq ???
    end
  end
end
doremi
  • 14,921
  • 30
  • 93
  • 148

1 Answers1

0

You shouldn't be testing the payload, you need to test that the method does what you expect with that payload. VCR is going to take care of storing the payload. You may need to assert that you send what you expect to the API, and assert what you do with the result. You should also test the fail scenarios, like the API times out, or network error etc. But the payload itself you shouldn't really need to touch in the test.

You will probably find that it helps to break the test down into scenarios;

  • given a call with the correct params
  • given a bad call e.g. missing resource at the API end
  • given a network error

I tend to find it easier to just write some of the obvious scenarios and then start from the assert for each one. Something like below, somewhat abridged and using RSpec but you should get the idea;

describe Transaction do
  # Create a real object in the api.
  let(:transaction) { create :transaction }

  describe '.find', :vcr do
    context 'given a valid id' do
      subject { Transaction.find(transaction.id) }

      it 'returns a Transaction object' do
        is_expected.to eq(transaction)
      end
    end

    context 'given an invalid transaction_id' do
      subject { Transaction.find('my-bad-transaction') }

      it 'should rescue Gateway::NotFoundError' do
        is_expected.to be_nil
      end

      it 'raises no NotFoundError' do
        expect { subject }.to raise_error(Gateway::NotFoundError)
      end
    end
  end
end
james2m
  • 1,562
  • 12
  • 15