Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 1.77 KB

README.md

File metadata and controls

51 lines (38 loc) · 1.77 KB

Coverage Status Build Status Go Report Card

HTTP Test Fixtures

Generate test fixtures that are real world responses!

When writing an api client I want to make sure the test fixtures that I am developing against are as close to real life as possible. So why not use curl to capture real world responses and use them as test fixtures?

  1. Generate the test fixtures.
$  curl https://cat-api.com/cats/1 \
     --include \
     --output "${PATH}/cats_id_exists"
     
HTTP/2 200 
server: nginx/1.18.0 (Ubuntu)
date: Mon, 27 Sep 2021 15:49:44 GMT
content-type: application/json; charset=UTF-8
vary: Accept-Encoding
access-control-allow-origin: *
cache-control: public, max-age=3600

{"data":{"id":1,"name":"Squeek","age":12}}
  1. Load the file into an *http.Response and set this to return from the mock.
func TestClient_GetCat_ID_Exists(t *testing.T) {
    res := httptestfixtures.MustLoadRequest(t, "./testdata/cats_id_exists")
    mockClient.On("Do", mock.Anything).Return(res, nil)
  
    // initialise the client
    sut := API{
      client: mockClient
    }
    
    // run the function under test
    out, err := sut.GetCat(1)
    
    // assert
    //make your asertions

Social Media