Mi forma de probarlo sin usar los archivos .json
y utilizando assert
de testify
package utils
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
var (
pokemonFromApi = []byte(`{"id": 132,"name": "ditto","types": [{"slot": 1,"type": {"name": "normal"}}],"stats": [{"base_stat": 48,"stat": {"name": "hp"}},{"base_stat": 48,"stat": {"name": "attack"}},{"base_stat": 48,"stat": {"name": "defense"}},{"base_stat": 48,"stat": {"name": "special-attack"}},{"base_stat": 48,"stat": {"name": "special-defense"}},{"base_stat": 48,"stat": {"name": "speed"}}]}`)
pokemonResponseDTO = []byte(`{"Id":132,"Name":"Ditto","Type": "Normal","Stats":{"Hp": 48,"Attack": 48,"Defense": 48,"Speed": 48}}`)
)
func TestNewParsePokemonToDTO(t *testing.T) {
apiPokemon := entities.PokemonApiResponse{}
err := json.Unmarshal(pokemonFromApi, &apiPokemon)
assert.NoError(t, err)
parsePokemon, err := ParsePokemonToDTO(apiPokemon)
assert.NoError(t, err)
pokemonResponse := dto.Pokemon{}
err = json.Unmarshal(pokemonResponseDTO, &pokemonResponse)
assert.NoError(t, err)
assert.Equal(t, parsePokemon, pokemonResponse)
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?