forked from sohamkamani/blog_example__go_web_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_mock.go
40 lines (35 loc) · 979 Bytes
/
store_mock.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 (
"github.com/stretchr/testify/mock"
)
// The mock store contains additonal methods for inspection
type MockStore struct {
mock.Mock
}
func (m *MockStore) CreateBird(bird *Bird) error {
/*
When this method is called, `m.Called` records the call, and also
returns the result that we pass to it (which you will see in the
handler tests)
*/
rets := m.Called(bird)
return rets.Error(0)
}
func (m *MockStore) GetBirds() ([]*Bird, error) {
rets := m.Called()
/*
Since `rets.Get()` is a generic method, that returns whatever we pass to it,
we need to typecast it to the type we expect, which in this case is []*Bird
*/
return rets.Get(0).([]*Bird), rets.Error(1)
}
func InitMockStore() *MockStore {
/*
Like the InitStore function we defined earlier, this function
also initializes the store variable, but this time, it assigns
a new MockStore instance to it, instead of an actual store
*/
s := new(MockStore)
store = s
return s
}