package socialnetworkservicelogic import ( "app-cloudep-tweeting-service/gen_result/pb/tweeting" mocklib "app-cloudep-tweeting-service/internal/mock/lib" mockRepo "app-cloudep-tweeting-service/internal/mock/repository" "app-cloudep-tweeting-service/internal/svc" "context" "errors" "testing" "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" ) func TestGetFolloweeCountLogic_GetFolloweeCount(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockSocialNetworkRepository := mockRepo.NewMockSocialNetworkRepository(ctrl) mockValidate := mocklib.NewMockValidate(ctrl) svcCtx := &svc.ServiceContext{ SocialNetworkRepository: mockSocialNetworkRepository, Validate: mockValidate, } // 测试数据集 tests := []struct { name string input *tweeting.FollowCountReq prepare func() expectErr bool }{ { name: "ok", input: &tweeting.FollowCountReq{ Uid: "12345", }, prepare: func() { mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1) mockSocialNetworkRepository.EXPECT().GetFolloweeCount(gomock.Any(), "12345").Return(int64(10), nil).Times(1) }, expectErr: false, }, { name: "驗證失敗", input: &tweeting.FollowCountReq{ Uid: "", }, prepare: func() { mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1) }, expectErr: true, }, { name: "取得跟隨數量失敗", input: &tweeting.FollowCountReq{ Uid: "12345", }, prepare: func() { mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1) mockSocialNetworkRepository.EXPECT().GetFolloweeCount(gomock.Any(), "12345").Return(int64(0), errors.New("repository error")).Times(1) }, expectErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.prepare() logic := GetFolloweeCountLogic{ svcCtx: svcCtx, ctx: context.TODO(), } got, err := logic.GetFolloweeCount(tt.input) if tt.expectErr { assert.Error(t, err) } else { assert.NoError(t, err) assert.Equal(t, tt.input.Uid, got.Uid) } }) } }