-
Notifications
You must be signed in to change notification settings - Fork 0
/
duelingbot_test.go
48 lines (38 loc) · 1.05 KB
/
duelingbot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetRandomSpells(t *testing.T) {
spells := GetRandomSpells(3)
if len(spells.SpellArray) != 3 && spells.Error == "" {
t.Errorf("GetRandomSpells was incorrect, got: %d, want: %d.", len(spells.SpellArray), 3)
}
}
func TestCreateMatch(t *testing.T) {
numberOfPlayers := 3
//API endpoint setup
req, err := http.NewRequest("GET", "/newmatch/3", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(CreateMatch)
//Making the API call
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
//Unmarshalling and storing API response in the proper struct
matchDetails := make([]PlayerList, numberOfPlayers)
err = json.Unmarshal(rr.Body.Bytes(), &matchDetails)
if err != nil {
t.Fatal(err)
}
if len(matchDetails) != 3 {
t.Errorf("CreateMatch was incorrect, got: %d, want: %d.", len(matchDetails), 3)
}
}