go-tarantool icon indicating copy to clipboard operation
go-tarantool copied to clipboard

v3: refactor request/response mocking to use a unified MockDoer API

Open bigbes opened this issue 1 month ago • 0 comments

Currently, the test mocks for the Tarantool client use separate types for mocking requests and responses (e.g., a standalone ResponseMock). This makes test setup verbose and exposes internal implementation details that users shouldn’t need to manage.

We want to simplify and unify the mocking interface by introducing a single, fluent MockDoer that lets tests declaratively define expected responses while automatically capturing incoming requests.

Proposed API

Replace the current approach with a MockDoer interface like this:

type MockDoer interface {
    // Add a successful response with just payload data
    AddResponseOk1(data []interface{}) MockDoer

    // Add a successful response with explicit header and body
    AddResponseOk2(header tarantool.Header, body io.Reader) MockDoer

    // Add an error response (e.g., network or protocol error)
    AddResponseError(err error) MockDoer

    // Return all requests that were sent to this mock during the test
    Requests() []tarantool.Request
}

This design:

  • Uses method chaining for easy setup
  • Hides low-level response construction (e.g., no need to build full Response structs manually)

Example Usage

mockDoer := mock.NewDoer().
    AddResponseOk1([]interface{}{"OK"}).
    AddResponseError(io.EOF)

client := tarantool.NewClient(mockDoer)
_, err := client.Call("some.func", nil) // succeeds
_, err = client.Call("another.func", nil) // returns io.EOF

// Later in test:
assert.Len(t, mockDoer.Requests(), 2)
assert.Equal(t, "some.func", mockDoer.Requests()[0].(tarantool.CallRequest).FunctionName)

Related links and repos

  • convenient powerful mocks: https://github.com/DATA-DOG/go-sqlmock

bigbes avatar Oct 30 '25 09:10 bigbes