forked from ppai-plivo/ut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
40 lines (29 loc) · 842 Bytes
/
service_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/ppai-plivo/ut/mocks"
"github.com/ppai-plivo/ut/pkg/db"
lmocks "github.com/plivo/pkg/log/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestHandlerSuccess(t *testing.T) {
assert := require.New(t)
store := new(mocks.Store)
store.On("GetUserByID", mock.Anything, 123).Return(&db.User{
Name: "John Doe",
Email: "[email protected]",
}, nil)
logger := new(lmocks.Logger)
svc := New(store, logger)
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8080/123", nil)
assert.NotNil(req)
assert.Nil(err)
w := httptest.NewRecorder()
svc.HandleRequest(w, req)
assert.Equal(http.StatusOK, w.Code)
assert.Equal(w.Header().Get("Content-Type"), "application/json")
store.AssertExpectations(t)
}