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
| package biz_test
import (
"p15/mock/mrepo"
"p15/biz"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("UserUsecase", func() {
var userCase *biz.UserUsecase
var mUserRepo *mrepo.MockUserRepo
info := &biz.User{}
BeforeEach(func() {
mUserRepo = mrepo.NewMockUserRepo(ctl)
userCase = biz.NewUserUsecase(mUserRepo)
info = &biz.User{
ID: 3,
Phone: "15866547585",
Pwd: "admin123456",
Nickname: "hahaha",
Uname: "hahaha",
}
})
It("Create", func() {
// 这里不会在数据库中创建
mUserRepo.EXPECT().CreateUser(gomock.Any()).Return(info, nil)
l, err := userCase.Create(info)
Ω(err).ShouldNot(HaveOccurred())
Ω(err).ToNot(HaveOccurred())
Ω(l.ID).To(Equal(int64(3)))
Ω(l.Phone).To(Equal("15866547585"))
})
It("UserById", func() {
mUserRepo.EXPECT().GetUserById(gomock.Any()).Return(info, nil)
user, err := userCase.UserById(3)
Ω(err).ShouldNot(HaveOccurred())
Ω(user.Phone).Should(Equal("15866547585"))
})
})
var _ = Describe("User", func() {
var ro biz.UserRepo
var uD *biz.User
BeforeEach(func() {
ro = biz.NewUserRepo()
uD = &biz.User{
ID: 4,
Phone: "18655864568",
Pwd: "admin123",
Nickname: "hahaha",
Uname: "hahaha",
}
})
It("CreateUser", func() {
// 这里会真的在数据库中创建
ro.CreateUser(uD)
})
It("GetUserById", func() {
user, err := ro.GetUserById(4)
Ω(err).ShouldNot(HaveOccurred())
Ω(user.Phone).Should(Equal("18655864568"))
})
})
|