How are integration tests written for interacting with external API?

后端 未结 4 1219
慢半拍i
慢半拍i 2020-12-04 07:41

First up, where my knowledge is at:

Unit Tests are those which test a small piece of code (single methods, mostly).

Integration Tests ar

4条回答
  •  星月不相逢
    2020-12-04 07:50

    I've recently had to update a library because the api it connects to was updated.

    My knowledge isn't enough to explain in detail, but i learnt a great deal from looking at the code. https://github.com/gridiron-guru/FantasyDataAPI

    You can submit a request as you would normally to the api and then save that response as a json file, you can then use that as a mock.

    Have a look at the tests in this library which connects to an api using Guzzle.

    It mocks responses from the api, there's a good deal of information in the docs on how the testing works it might give you an idea of how to go about it.

    but basically you do a manual call to the api along with any parameters you need, and save the response as a json file.

    When you write your test for the api call, send along the same parameters and get it to load in the mock rather than using the live api, you can then test the data in the mock you created contains the expected values.

    My Updated version of the api in question can be found here. Updated Repo

提交回复
热议问题