package main import ( "encoding/json" "fmt" "io" "net/http" "net/http/httptest" "reflect" "testing" "git.andreafazzi.eu/andrea/testhub/models" "github.com/remogatto/prettytest" ) // Start of setup type testSuite struct { prettytest.Suite } type StubQuestionsStore struct { questions map[uint]*models.Question answers map[uint]*models.Answer lastQuestionID uint lastAnswerID uint } func (store *StubQuestionsStore) CreateQuestion(question *models.Question) *models.Question { } func (store *StubQuestionsStore) GetQuestions() []*models.Question { return store.questions } func TestRunner(t *testing.T) { prettytest.Run( t, new(testSuite), ) } func (t *testSuite) TestGETQuestions() { expectedResult := []*models.Question{ &{ Text: "Domanda 1", Answers: []*models.Answers{&{Text: "Option 1"},&{Text: "Option 2"}, &{Text: "Option 3"}, }, } store := &StubPlayerStore{ nil, nil, expectedResult, } server := NewPlayerServer(store) request, _ := http.NewRequest(http.MethodGet, "/league", nil) response := httptest.NewRecorder() server.ServeHTTP(response, request) result := getLeagueFromResponse(response.Body) t.True(leaguesAreEqual(expectedResult, result)) t.Equal(http.StatusOK, response.Code) } func getLeagueFromResponse(body io.Reader) (league []models.Player) { err := json.NewDecoder(body).Decode(&league) if err != nil { panic(fmt.Errorf("Unable to parse response from server %q into slice of Player, '%v'", body, err)) } return } func leaguesAreEqual(got, want []models.Player) bool { return reflect.DeepEqual(got, want) }