1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| func TestUserHandler_SignUp(t *testing.T) { testCases := []struct { name string mock func(ctrl *gomock.Controller) (service.UserService, service.CodeService) reqBody string wantCode int wantBody string }{ { name: "注册成功", mock: func(ctrl *gomock.Controller) (service.UserService, service.CodeService) { userSvc := svcmocks.NewMockUserService(ctrl) userSvc.EXPECT().SignUp(gomock.Any(), domain.User{ Email: "[email protected]", Password: "qwer#1234", }).Return(nil) codeSvc := svcmocks.NewMockCodeService(ctrl) return userSvc, codeSvc }, reqBody: `{ "email": "[email protected]", "password": "qwer#1234" }`, wantCode: http.StatusOK, wantBody: "注册成功", }, { name: "邮箱格式不正确", mock: func(ctrl *gomock.Controller) (service.UserService, service.CodeService) { userSvc := svcmocks.NewMockUserService(ctrl) codeSvc := svcmocks.NewMockCodeService(ctrl) return userSvc, codeSvc }, reqBody: `{ "email": "hahaqq.com", "password": "qwer#1234" }`, wantCode: http.StatusBadRequest, wantBody: "邮箱格式不对", }, { name: "参数错误,Bind失败", mock: func(ctrl *gomock.Controller) (service.UserService, service.CodeService) { userSvc := svcmocks.NewMockUserService(ctrl) codeSvc := svcmocks.NewMockCodeService(ctrl) return userSvc, codeSvc }, reqBody: `{ email": "hahaqq.com", "password": "qwer#1234" }`, wantCode: http.StatusBadRequest, wantBody: "参数错误", }, { name: "邮箱冲突", mock: func(ctrl *gomock.Controller) (service.UserService, service.CodeService) { userSvc := svcmocks.NewMockUserService(ctrl) userSvc.EXPECT().SignUp(gomock.Any(), domain.User{ Email: "[email protected]", Password: "qwer#1234", }).Return(service.ErrDuplicateEmail) codeSvc := svcmocks.NewMockCodeService(ctrl) return userSvc, codeSvc }, reqBody: `{ "email": "[email protected]", "password": "qwer#1234" }`, wantCode: http.StatusInternalServerError, wantBody: "邮箱冲突", }, { name: "系统错误", mock: func(ctrl *gomock.Controller) (service.UserService, service.CodeService) { userSvc := svcmocks.NewMockUserService(ctrl) userSvc.EXPECT().SignUp(gomock.Any(), domain.User{ Email: "[email protected]", Password: "qwer#1234", }).Return(errors.New("随便一个异常")) codeSvc := svcmocks.NewMockCodeService(ctrl) return userSvc, codeSvc }, reqBody: `{ "email": "[email protected]", "password": "qwer#1234" }`, wantCode: http.StatusInternalServerError, wantBody: "系统错误", }, { name: "密码必须大于8位,包含数字、特殊字符", mock: func(ctrl *gomock.Controller) (service.UserService, service.CodeService) { userSvc := svcmocks.NewMockUserService(ctrl) codeSvc := svcmocks.NewMockCodeService(ctrl) return userSvc, codeSvc }, reqBody: `{ "email": "[email protected]", "password": "qwer1234" }`, wantCode: http.StatusBadRequest, wantBody: "密码必须大于8位,包含数字、特殊字符", }, }
for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()
server := gin.Default() userService, codeService := tc.mock(ctrl) handler := NewUserHandler(userService, codeService) handler.RegisterRouters(server)
request, err := http.NewRequest(http.MethodPost, "/users/signup", bytes.NewReader([]byte(tc.reqBody))) request.Header.Set("Content-Type", "application/json") assert.NoError(t, err) recorder := httptest.NewRecorder() server.ServeHTTP(recorder, request) assert.Equal(t, tc.wantCode, recorder.Code) assert.Equal(t, tc.wantBody, recorder.Body.String()) }) } }
|