Created
June 18, 2024 08:05
-
-
Save kvii/b2047f03098390c2c83d2f3e606f71dc to your computer and use it in GitHub Desktop.
testing for kratos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package biz | |
import ( | |
"context" | |
"io" | |
"reflect" | |
"testing" | |
"time" | |
"github.com/go-kratos/kratos/v2/log" | |
) | |
func TestUserUsecase_Login(t *testing.T) { | |
var fakeTime = time.Date(2006, 1, 2, 15, 4, 5, 0, time.Local) | |
testCases := []struct { | |
name string // 测试项 | |
create func() *UserUsecase // 构造函数 | |
ctx context.Context // 参数 1 | |
req LoginReq // 参数 2 | |
want *LoginInfo // 期望返回值 | |
err error // 期望错误 | |
}{ | |
{ | |
name: "normal", | |
create: func() *UserUsecase { | |
return NewUserUsecase( | |
userRepoStub{ | |
FindFn: func(ctx context.Context, name string) (*UserInfo, error) { | |
return &UserInfo{ | |
Id: 1, | |
UserName: "u", | |
Password: "p", | |
CreatedAt: fakeTime, | |
UpdatedAt: fakeTime, | |
}, nil | |
}, | |
}, | |
&signRepoStub{ | |
signFn: func(u *UserInfo) (string, error) { | |
return "token", nil | |
}, | |
}, | |
log.NewStdLogger(io.Discard), | |
) | |
}, | |
ctx: context.Background(), | |
req: LoginReq{ | |
Name: "u", | |
Password: "p", | |
}, | |
want: &LoginInfo{ | |
Id: 1, | |
UserName: "u", | |
Token: "token", | |
}, | |
err: nil, | |
}, | |
// 在编写服务前就要提前设想一些边界情况,把这些情况写进用例中。 | |
// 文档注释中写到的边界情况都添加进来,保证服务与文档描述一致。出问题时能迅速定位问题。 | |
// 线上出问题后,把当时的请求参数流下来当测试用例。修复后,以后就不会再出现这个问题了。 | |
} | |
for _, tc := range testCases { | |
t.Run(tc.name, func(t *testing.T) { | |
uc := tc.create() | |
info, err := uc.Login(tc.ctx, tc.req) | |
if err != tc.err { | |
t.Errorf("err != tc.err. want: %v, got: %v", tc.err, err) | |
} | |
if !reflect.DeepEqual(info, tc.want) { | |
t.Errorf("info != tc.want. want: %v, got: %v", tc.want, info) | |
} | |
}) | |
} | |
} | |
type userRepoStub struct { | |
SaveFn func(ctx context.Context, req *UserInfo) (*UserInfo, error) | |
UpdateFn func(ctx context.Context, req *UserInfo) (*UserInfo, error) | |
FindByIDFn func(ctx context.Context, req int64) (*UserInfo, error) | |
ListByHelloFn func(ctx context.Context, req string) ([]*UserInfo, error) | |
ListAllFn func(ctx context.Context) ([]*UserInfo, error) | |
FindFn func(ctx context.Context, name string) (*UserInfo, error) | |
DetailFn func(ctx context.Context, name, area string) (*UserDetail, error) | |
PublicInfoListFn func(ctx context.Context, idList []int) ([]PublicInfo, error) | |
} | |
func (r userRepoStub) Save(ctx context.Context, arg *UserInfo) (*UserInfo, error) { | |
return r.SaveFn(ctx, arg) | |
} | |
func (r userRepoStub) Update(ctx context.Context, arg *UserInfo) (*UserInfo, error) { | |
return r.UpdateFn(ctx, arg) | |
} | |
func (r userRepoStub) FindByID(ctx context.Context, arg int64) (*UserInfo, error) { | |
return r.FindByIDFn(ctx, arg) | |
} | |
func (r userRepoStub) ListByHello(ctx context.Context, arg string) ([]*UserInfo, error) { | |
return r.ListByHelloFn(ctx, arg) | |
} | |
func (r userRepoStub) ListAll(ctx context.Context) ([]*UserInfo, error) { | |
return r.ListAllFn(ctx) | |
} | |
func (r userRepoStub) Find(ctx context.Context, name string) (*UserInfo, error) { | |
return r.FindFn(ctx, name) | |
} | |
func (r userRepoStub) PublicInfoList(ctx context.Context, idList []int) ([]PublicInfo, error) { | |
return r.PublicInfoListFn(ctx, idList) | |
} | |
// Detail 方法查询用户详情 | |
func (r userRepoStub) Detail(ctx context.Context, name, area string) (*UserDetail, error) { | |
return r.DetailFn(ctx, name, area) | |
} | |
type signRepoStub struct { | |
signFn func(u *UserInfo) (string, error) | |
} | |
func (r *signRepoStub) Sign(u *UserInfo) (string, error) { | |
return r.signFn(u) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment