/
repository_mock.go
62 lines (48 loc) · 1.64 KB
/
repository_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package mocks
import (
"github.com/gilperopiola/go-mocks-architecture-poc/example/core"
"github.com/stretchr/testify/mock"
)
// RepositoryMock is a mock of Repository interface.
type RepositoryMock struct {
*mock.Mock
}
// NewRepositoryMock returns a new mock of the Repository interface.
func NewRepositoryMock() *RepositoryMock {
return &RepositoryMock{&mock.Mock{}}
}
/* ----------------------------------- */
/* - Get User Mock - */
/* ----------------------------------- */
func (m *RepositoryMock) GetUser(id int) (*core.User, error) {
args := m.Called(id)
return args.Get(0).(*core.User), args.Error(1)
}
type repositoryGetUserValues struct {
UserID int
Response *core.User
Error error
}
func SetupRepositoryWithGetUser(option string) *RepositoryMock {
optionValues := Options.RepositoryGetUser[option]
repositoryMock := NewRepositoryMock()
repositoryMock.On("GetUser", optionValues.UserID).Return(optionValues.Response, optionValues.Error).Once()
return repositoryMock
}
/* ----------------------------------- */
/* - Is User Valid Mock - */
/* ----------------------------------- */
func (m *RepositoryMock) IsUserValid(id int) bool {
args := m.Called(id)
return args.Bool(0)
}
type repositoryIsUserValidValues struct {
UserID int
Valid bool
}
// SetupRepositoryWithIsUserValid takes a mock as parameter because a previous mocked call to the GetUser method is needed.
func SetupRepositoryWithIsUserValid(mock *RepositoryMock, option string) *RepositoryMock {
optionValues := Options.RepositoryIsUserValid[option]
mock.On("IsUserValid", optionValues.UserID).Return(optionValues.Valid).Once()
return mock
}