If you don't know what exact data you are asserting in Golang unit tests, you can use a regex pattern placeholder as shown below. This example expects a numeric ID data as part of response body but we don't know what it is so using ID_REGEX as placeholder.


Example


package league

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)

func TestCreate(t *testing.T) {
// Some other code.

requestBody := `
{
"name": "Premier League"
}
`

responseBody := `
{
"operation": "leagues_post",
"data": {
"id": ID_REGEX
}
}
`

rq := httptest.NewRequest(http.MethodPost, "/leagues", strings.NewReader(requestBody))
rw := httptest.NewRecorder()

yourHandler.Create(rw, rq)

// Flatten formatted json string.
buffer := new(bytes.Buffer)
_ = json.Compact(buffer, []byte(responseBody))

// Replace placeholders with corresponding regex patterns.
expectedBody := strings.ReplaceAll(buffer.String(), "ID_REGEX", "\\d+/g") // Or "\\w+" for strings

assert.Regexp(t, expectedBody, rw.Body.String())

// Some other code.
}