vendredi 6 mars 2020

How to mock complex api response objects

I am working with the Braintree SDK in order to implement PayPal server side payments. In order to account for all scenarios when submitting a payment such as declined payments etc. I want to write tests using a mocked instance of the API class 'Braintree_Gateway'. The responses of that API are heavily nested and rather complex objects.

For mocking these responses I see two options:

  1. Capturing a real response object, serialising it and then loading it from memory for testing
  2. Creating and nesting the objects myself by instantiating the Response class

Is there a third way that I haven't considered yet?

Number 1 has the advantage, that I test against an object that the API actually returned which means that I test against the 'real thing'. Also it's rather quick to set up

Number 2 has the advantage that the structure of the object is more exposed in my test and therefore providing a better reference to what the response is supposed to look like in every case. Disadvantage is that it's tedious to setup and tedious to maintain.

What is the correct way of doing this, considering maintainability as well as integrity of the tests?

Aucun commentaire:

Enregistrer un commentaire