-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
58 lines (45 loc) · 1.46 KB
/
main_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
49
50
51
52
53
54
55
56
57
58
package main
import (
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"github.com/jianghushinian/blog-go-example/test/mysql/store"
"github.com/jianghushinian/blog-go-example/test/mysql/store/mocks"
)
func TestUserHandler_CreateUser_by_mock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockUserStore := mocks.NewMockUserStore(ctrl)
mockUserStore.EXPECT().Create(&store.User{
Name: "user1",
}).Return(nil)
handler := &UserHandler{store: mockUserStore}
router := setupRouter(handler)
w := httptest.NewRecorder()
body := `{"name": "user1"}`
reader := strings.NewReader(body)
req := httptest.NewRequest("POST", "/users", reader)
router.ServeHTTP(w, req)
assert.Equal(t, 201, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, "", w.Body.String())
}
func TestUserHandler_GetUser_by_mock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockUserStore := mocks.NewMockUserStore(ctrl)
mockUserStore.EXPECT().Get(2).Return(&store.User{
ID: 2,
Name: "user2",
}, nil)
handler := &UserHandler{store: mockUserStore}
router := setupRouter(handler)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/users/2", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, `{"id":2,"name":"user2"}`, w.Body.String())
}