-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_repository_test.go
72 lines (55 loc) · 1.38 KB
/
user_repository_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
package repository_testing
import "testing"
// Declaring testing required variables
var (
id interface{}
name interface{}
username interface{}
)
// Mocking dbQueryBuilder
type MockDbQueryBuilder struct {
t *testing.T
}
// Mocked QueryRow method
func (self *MockDbQueryBuilder) QueryRow(query string, args ...interface{}) {
// assert
if query != "select * from users where id = $1" {
self.t.Error("Query string should be 'select * from users where id = $1'")
}
// assert
if len(args) != 1 {
self.t.Error("One argument should be passed for the query string")
}
}
// Mocked Scan method
func (self *MockDbQueryBuilder) Scan(dest ...interface{}) error {
// assert
if len(dest) != 3 {
self.t.Error("Three arguments must be passed")
}
// storing arguments memory address
// for the next assertion
id = dest[0]
name = dest[1]
username = dest[2]
return nil
}
func TestGetUser(t *testing.T) {
t.Run("It should return a user", func(t *testing.T) {
// setup
dbQueryBuilder = &MockDbQueryBuilder{t: t}
ur := new(UserRepository)
// act
user := ur.GetUser(1)
// assert
if &user.Id != id {
t.Error("Passing wrong memory address for the user's id")
}
if &user.Name != name {
t.Error("Passing wrong memory address for the user's name")
}
if &user.Username != username {
t.Error("Passing wrong memory address for the user's username")
}
})
}