Skip to content

Generating Recurly API Errors for testing #937

@timhugh

Description

@timhugh

It's currently a little bit difficult/haphazard to create specific errors when stubbing the Recurly gem for tests. I've been doing something similar to this:

def build_recurly_api_error(type, message, params)
  error = Recurly::Resources::Error.new(type: type, message: message, params: Array.wrap(params))
  Recurly::Errors::APIError.new(message, nil, error)
end

it 'handles errors from recurly' do
  error = build_recurly_api_error('validation', 'Account is invalid', { 'param' => 'account', 'message' => 'Account is invalid' })
  allow(mock_recurly_client).to receive(:create_purchase).and_raise(error)
  # ...
end

That's a contrived error, but the point is that it would be really nice to have factories available from the gem to generate errors that I know have the correct attributes and parameters so I can be confident that I'm handling them correctly:

Recurly::Test.generate_validation_error(field: :country, message: "must be valid ISO country")
Recurly::Test.generate_processing_error('card was declined')

I've noticed in manual testing, for example, that some errors will look like

{
  message: 'Country was invalid',
  params: { field: 'country', message: 'a country error' }
}

but other errors will only have

{
  message: 'Country was invalid',
  params: { field: 'country' }
}

so I've had to implement some fallback logic for where to find an actual error message, but it feels a little bit like a shot in the dark.

These examples are admittedly vague, so if more concrete examples would help let me know and I can try to induce some.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions