-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinterface_test.go
43 lines (35 loc) · 893 Bytes
/
interface_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
// This package tests the Interface injection
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
// Testing the Interface Injection
// The API resource has an Action called GETDogBark
// It depends on an Interface Dogger
// The interface is implemented by the Maltese struct attached to the API
func TestInterfaceInjection(t *testing.T) {
rt, err := NewRouter(api)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/api/dogbark", nil)
if err != nil {
t.Fatal(err)
}
rt.ServeHTTP(w, req)
// Try to get the gopher from the response
var resp StringResp
err = json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatal(err)
}
maltese := Maltese{}
// Testing if the interface implementation injection
if resp.String != maltese.Bark() {
t.Fatal("Interface not injected correctly")
}
}