From 8e0473c418640aaf872c116bf1c9f19733327fd5 Mon Sep 17 00:00:00 2001 From: haopeng <657407891@qq.com> Date: Sun, 8 Dec 2024 19:34:15 +0800 Subject: [PATCH] tests: add mock redis (#37) --- biz/service/user/send_code_test.go | 24 ++++++++++++++++-------- go.mod | 3 +++ go.sum | 6 ++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/biz/service/user/send_code_test.go b/biz/service/user/send_code_test.go index 5054a32..81223b4 100644 --- a/biz/service/user/send_code_test.go +++ b/biz/service/user/send_code_test.go @@ -4,18 +4,16 @@ import ( "context" "testing" - "xzdp/biz/dal/mysql" - "xzdp/biz/dal/redis" - + redis2 "xzdp/biz/dal/redis" user "xzdp/biz/model/user" + "github.com/alicebob/miniredis/v2" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/common/test/assert" + "github.com/go-redis/redis/v8" ) func TestSendCodeService_Run(t *testing.T) { - // redis.Init() - ctx := context.Background() c := app.NewContext(1) s := NewSendCodeService(ctx, c) @@ -23,14 +21,24 @@ func TestSendCodeService_Run(t *testing.T) { req := &user.UserLoginFrom{ Phone: "12345678901"} resp, err := s.Run(req) + assert.Nil(t, resp) + assert.NotNil(t, err) + + resp, err = s.Run(&user.UserLoginFrom{ + Phone: "13412332123"}) assert.DeepEqual(t, &user.Result{Success: true}, resp) assert.DeepEqual(t, nil, err) - // todo edit your unit test. } func TestMain(m *testing.M) { - redis.Init() - mysql.Init() + s, err := miniredis.Run() + if err != nil { + panic(err) + } + defer s.Close() + redis2.RedisClient = redis.NewClient(&redis.Options{ + Addr: s.Addr(), // mock redis server的地址 + }) m.Run() } diff --git a/go.mod b/go.mod index afd2a2b..b0585b3 100755 --- a/go.mod +++ b/go.mod @@ -19,6 +19,8 @@ require ( ) require ( + github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect + github.com/alicebob/miniredis/v2 v2.33.0 // indirect github.com/bytedance/go-tagexpr/v2 v2.9.2 // indirect github.com/bytedance/gopkg v0.0.0-20240507064146-197ded923ae3 // indirect github.com/bytedance/sonic v1.11.9 // indirect @@ -47,6 +49,7 @@ require ( github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/yuin/gopher-lua v1.1.1 // indirect golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect golang.org/x/sys v0.5.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index 192af95..692877f 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk= +github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= +github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA= +github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0= github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI= github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/bytedance/go-tagexpr/v2 v2.9.2 h1:QySJaAIQgOEDQBLS3x9BxOWrnhqu5sQ+f6HaZIxD39I= @@ -125,6 +129,8 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= +github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=