我如何测试这个函数
我需要确保模拟的 FindByID 函数返回值是具有创建 UUID 的用户。
func (s *Service) Register(newUser domain.User) (domain.User, error) {
newUser.ID = uuid.New()
s.repo.Create(newUser)
createdUser, err := s.FindByID(newUser.ID)
if err != nil {
return domain.User{}, err
}
return createdUser, nil
}
目前测试功能
t.Run("When Register called, it should return createdUser", func(t *testing.T) {
repo := new(mocks.UserRepository)
s := service.NewService(repo)
user := domain.User{Name: "TestName", Email: "TestEmail"}
repo.On("Create", mock.Anything)
// I need to make sure FindByID function return value is a user with created UUID
repo.On("FindByID", mock.Anything).Return(user, nil)
createdUser, err := s.Register(user)
assert.NoError(t, err)
assert.IsType(t, createdUser.ID, uuid.UUID{})
})