Sobes.tech
Junior — Middle

¿Qué bibliotecas utilizaste para crear objetos simulados en las pruebas?

sobes.tech AI

Respuesta de la IA

En Go, para crear objetos simulados en las pruebas, a menudo se utilizan las siguientes bibliotecas:

  • gomock — biblioteca oficial de Google, permite generar mocks basados en interfaces mediante la herramienta mockgen.
  • testify/mock — parte del popular paquete testify, proporciona una forma conveniente de crear mocks con la capacidad de establecer expectativas y verificar llamadas.

Ejemplo de uso de testify/mock:

import (
    "testing"
    "github.com/stretchr/testify/mock"
)

type MyInterface interface {
    DoSomething(int) string
}

type MyMock struct {
    mock.Mock
}

func (m *MyMock) DoSomething(i int) string {
    args := m.Called(i)
    return args.String(0)
}

func TestMyFunction(t *testing.T) {
    m := new(MyMock)
    m.On("DoSomething", 123).Return("result")

    res := m.DoSomething(123)
    if res != "result" {
        t.Errorf("resultado inesperado: %s", res)
    }

    m.AssertExpectations(t)
}

Estas bibliotecas ayudan a aislar el código probado de dependencias externas y a verificar las interacciones con ellas.