package commentservicelogic import ( "app-cloudep-tweeting-service/gen_result/pb/tweeting" "app-cloudep-tweeting-service/internal/svc" "context" "errors" "testing" "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" mockmodel "app-cloudep-tweeting-service/internal/mock/model" ) func TestDeleteComment(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() // 初始化 mock 依賴 mockCommentModel := mockmodel.NewMockCommentModel(ctrl) // 初始化服務上下文 svcCtx := &svc.ServiceContext{ CommentModel: mockCommentModel, } // 測試數據 commentReq := &tweeting.DeleteCommentReq{ CommentId: []string{"12345", "67890"}, } // 測試數據集 tests := []struct { name string input *tweeting.DeleteCommentReq prepare func() expectErr bool }{ { name: "成功刪除評論", input: commentReq, prepare: func() { // 模擬 DeleteMany 成功 mockCommentModel.EXPECT().DeleteMany(gomock.Any(), "12345", "67890").Return(int64(2), nil).Times(1) }, expectErr: false, }, { name: "刪除評論失敗", input: commentReq, prepare: func() { // 模擬 DeleteMany 失敗 mockCommentModel.EXPECT().DeleteMany(gomock.Any(), "12345", "67890").Return(int64(0), errors.New("delete failed")).Times(1) }, expectErr: true, }, } // 執行測試 for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // 設置測試環境 tt.prepare() // 初始化 DeleteCommentLogic logic := DeleteCommentLogic{ svcCtx: svcCtx, ctx: context.TODO(), } // 執行 DeleteComment resp, err := logic.DeleteComment(tt.input) // 驗證結果 if tt.expectErr { assert.Error(t, err) assert.Nil(t, resp) } else { assert.NoError(t, err) assert.NotNil(t, resp) } }) } }