@@ -19,14 +19,20 @@ import (
1919// 用于隔离测试 AccountService.Delete 方法,避免依赖真实数据库。
2020//
2121// 设计说明:
22+ // - account/getErr: 模拟 GetByID 返回的账号和错误
2223// - exists: 模拟 ExistsByID 返回的存在性结果
2324// - existsErr: 模拟 ExistsByID 返回的错误
2425// - deleteErr: 模拟 Delete 返回的错误
26+ // - getIDs/existsIDs: 记录查询调用的账号 ID,用于断言验证
2527// - deletedIDs: 记录被调用删除的账号 ID,用于断言验证
2628type accountRepoStub struct {
29+ account * Account
30+ getErr error
2731 exists bool // ExistsByID 的返回值
2832 existsErr error // ExistsByID 的错误返回值
2933 deleteErr error // Delete 的错误返回值
34+ getIDs []int64 // 记录已查询的账号 ID 列表
35+ existsIDs []int64 // 记录已检查存在性的账号 ID 列表
3036 deletedIDs []int64 // 记录已删除的账号 ID 列表
3137}
3238
@@ -37,16 +43,18 @@ func (s *accountRepoStub) Create(ctx context.Context, account *Account) error {
3743}
3844
3945func (s * accountRepoStub ) GetByID (ctx context.Context , id int64 ) (* Account , error ) {
40- panic ("unexpected GetByID call" )
46+ s .getIDs = append (s .getIDs , id )
47+ return s .account , s .getErr
4148}
4249
4350func (s * accountRepoStub ) GetByIDs (ctx context.Context , ids []int64 ) ([]* Account , error ) {
4451 panic ("unexpected GetByIDs call" )
4552}
4653
4754// ExistsByID 返回预设的存在性检查结果。
48- // 这是 Delete 方法调用的第一个仓储方法,用于验证账号是否存在 。
55+ // Delete 方法会在 GetByID 失败时使用它作为兼容性兜底 。
4956func (s * accountRepoStub ) ExistsByID (ctx context.Context , id int64 ) (bool , error ) {
57+ s .existsIDs = append (s .existsIDs , id )
5058 return s .exists , s .existsErr
5159}
5260
@@ -209,7 +217,7 @@ func (s *accountRepoStub) ResetQuotaUsed(ctx context.Context, id int64) error {
209217
210218// TestAccountService_Delete_NotFound 测试删除不存在的账号时返回正确的错误。
211219// 预期行为:
212- // - ExistsByID 返回 false (账号不存在)
220+ // - GetByID 返回 nil (账号不存在)
213221// - 返回 ErrAccountNotFound 错误
214222// - Delete 方法不被调用(deletedIDs 为空)
215223func TestAccountService_Delete_NotFound (t * testing.T ) {
@@ -218,21 +226,29 @@ func TestAccountService_Delete_NotFound(t *testing.T) {
218226
219227 err := svc .Delete (context .Background (), 55 )
220228 require .ErrorIs (t , err , ErrAccountNotFound )
229+ require .Equal (t , []int64 {55 }, repo .getIDs )
230+ require .Empty (t , repo .existsIDs )
221231 require .Empty (t , repo .deletedIDs ) // 验证删除操作未被调用
222232}
223233
224234// TestAccountService_Delete_CheckError 测试存在性检查失败时的错误处理。
225235// 预期行为:
236+ // - GetByID 返回错误
226237// - ExistsByID 返回数据库错误
227238// - 返回包含 "check account" 的错误信息
228239// - Delete 方法不被调用
229240func TestAccountService_Delete_CheckError (t * testing.T ) {
230- repo := & accountRepoStub {existsErr : errors .New ("db down" )}
241+ repo := & accountRepoStub {
242+ getErr : errors .New ("get failed" ),
243+ existsErr : errors .New ("db down" ),
244+ }
231245 svc := & AccountService {accountRepo : repo }
232246
233247 err := svc .Delete (context .Background (), 55 )
234248 require .Error (t , err )
235249 require .ErrorContains (t , err , "check account" ) // 验证错误信息包含上下文
250+ require .Equal (t , []int64 {55 }, repo .getIDs )
251+ require .Equal (t , []int64 {55 }, repo .existsIDs )
236252 require .Empty (t , repo .deletedIDs )
237253}
238254
@@ -244,6 +260,7 @@ func TestAccountService_Delete_CheckError(t *testing.T) {
244260// - deletedIDs 记录了尝试删除的 ID
245261func TestAccountService_Delete_DeleteError (t * testing.T ) {
246262 repo := & accountRepoStub {
263+ account : & Account {ID : 55 },
247264 exists : true ,
248265 deleteErr : errors .New ("delete failed" ),
249266 }
@@ -252,6 +269,8 @@ func TestAccountService_Delete_DeleteError(t *testing.T) {
252269 err := svc .Delete (context .Background (), 55 )
253270 require .Error (t , err )
254271 require .ErrorContains (t , err , "delete account" )
272+ require .Equal (t , []int64 {55 }, repo .getIDs )
273+ require .Empty (t , repo .existsIDs )
255274 require .Equal (t , []int64 {55 }, repo .deletedIDs ) // 验证删除操作被调用
256275}
257276
@@ -262,10 +281,12 @@ func TestAccountService_Delete_DeleteError(t *testing.T) {
262281// - 返回 nil 错误
263282// - deletedIDs 记录了被删除的 ID
264283func TestAccountService_Delete_Success (t * testing.T ) {
265- repo := & accountRepoStub {exists : true }
284+ repo := & accountRepoStub {account : & Account { ID : 55 } }
266285 svc := & AccountService {accountRepo : repo }
267286
268287 err := svc .Delete (context .Background (), 55 )
269288 require .NoError (t , err )
289+ require .Equal (t , []int64 {55 }, repo .getIDs )
290+ require .Empty (t , repo .existsIDs )
270291 require .Equal (t , []int64 {55 }, repo .deletedIDs ) // 验证正确的 ID 被删除
271292}
0 commit comments