-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
94 lines (64 loc) · 1.83 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// http://www.markjberger.com/testing-web-apps-in-golang/
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/youcodeio/proxy/database"
)
func TestStub(t *testing.T) {
assert.True(t, true, "This is good. Canary test passing")
}
type MyMockedDB struct {
mock.Mock
}
func (m *MyMockedDB) getChannelsFromDB() []database.Channel {
args := m.Called()
return args.Get(0).([]database.Channel)
}
/**
var (
server *httptest.Server
)
func init() {
mockedDB := new(MyMockedDB)
server = httptest.NewServer(api.NewRouter(mockedDB)) //Creating new server
}
func TestSlash(t *testing.T) {
assert := assert.New(t)
request, err := http.NewRequest("GET", fmt.Sprintf("%s", server.URL), nil)
res, err := http.DefaultClient.Do(request)
assert.Nil(err)
assert.Equal(res.StatusCode, 404, "Should be HTTP 404")
}
func TestChannelslist(t *testing.T) {
assert := assert.New(t)
request, err := http.NewRequest("GET", fmt.Sprintf("%s/channels", server.URL), nil)
res, err := http.DefaultClient.Do(request)
assert.Nil(err)
assert.Equal(res.StatusCode, 200, "Should be HTTP 200")
var data []database.Channel
defer res.Body.Close()
// Read the content into a byte array
body, err := ioutil.ReadAll(res.Body)
assert.Nil(err)
err = json.Unmarshal(body, &data)
assert.Nil(err)
}
type MyMockedDB struct {
*database.YouCodeDB
mock.Mock
}
func TestChannels(t *testing.T) {
var channels []database.Channel
ch := database.Channel{1, "name1", "id1", false}
channels = append(channels, ch)
ch = database.Channel{2, "name1", "id1", false}
channels = append(channels, ch)
mockedDB := new(MyMockedDB)
mockedDB.On("getChannelsFromDB").Return(channels)
r, err := http.NewRequest("GET", "employees/1", nil)
w := httptest.NewRecorder()
api.NewRouter(mockedDB).ServeHTTP(w, r)
}
**/