How to use MinTimes method of gomock Package

Best Mock code snippet using gomock.MinTimes

messages_handlers_test.go

Source:messages_handlers_test.go Github

copy

Full Screen

...33 Body: "hey cutie :)",34 IsRead: false,35 SendAt: time.Time{},36 },37 }, nil).MinTimes(1)38 },39 ExpectedCode: http.StatusOK,40 ExpectedBody: gin.H{"result": []gin.H{41 {42 "id": "000000000000000000000000",43 "from": "iskralawrence",44 "to": "aliparlakci",45 "body": "hey cutie :)",46 "is_read": false,47 "send_at": time.Time{},48 },49 }},50 }, {51 Prepare: func(getter *mocks.MockMessageGetter) {52 getter.EXPECT().GetAllMessages(gomock.Any(), "johndoe").Return(nil, errors.New("")).MinTimes(1)53 },54 ExpectedCode: http.StatusInternalServerError,55 ExpectedBody: gin.H{},56 },57 }58 for i, tt := range tests {59 testName := fmt.Sprintf("[%v]", i)60 t.Run(testName, func(t *testing.T) {61 ctrl := gomock.NewController(t)62 defer ctrl.Finish()63 mockedMessageGetter := mocks.NewMockMessageGetter(ctrl)64 if tt.Prepare != nil {65 tt.Prepare(mockedMessageGetter)66 }67 recorder := httptest.NewRecorder()68 _, r := gin.CreateTestContext(recorder)69 r.Use(func(c *gin.Context) {70 c.Set("user", models.User{Username: "johndoe"})71 })72 r.GET("/api/messages", GetAllMessages(mockedMessageGetter))73 request, err := http.NewRequest(http.MethodGet, "/api/messages", nil)74 if err != nil {75 t.Fatal(err)76 }77 r.ServeHTTP(recorder, request)78 if bodyAssertion, err := common.AreBodiesEqual(tt.ExpectedBody, recorder.Result().Body); err != nil {79 t.Fatal(err)80 } else if !bodyAssertion {81 t.Errorf("response bodies don't match")82 }83 if recorder.Result().StatusCode != tt.ExpectedCode {84 t.Errorf("want %v, got %v", tt.ExpectedCode, recorder.Result().StatusCode)85 }86 })87 }88}89func TestGetNewMessages(t *testing.T) {90 tests := []struct {91 Prepare func(getter *mocks.MockMessageGetter)92 ExpectedCode int93 ExpectedBody gin.H94 }{95 {96 Prepare: func(getter *mocks.MockMessageGetter) {97 getter.EXPECT().GetNewMessages(gomock.Any(), "johndoe").Return([]models.Message{98 {99 ID: primitive.ObjectID{},100 From: "iskralawrence",101 To: "aliparlakci",102 Body: "hey cutie :)",103 IsRead: false,104 SendAt: time.Time{},105 },106 }, nil).MinTimes(1)107 },108 ExpectedCode: http.StatusOK,109 ExpectedBody: gin.H{"result": []gin.H{110 {111 "id": "000000000000000000000000",112 "from": "iskralawrence",113 "to": "aliparlakci",114 "body": "hey cutie :)",115 "is_read": false,116 "send_at": time.Time{},117 },118 }},119 }, {120 Prepare: func(getter *mocks.MockMessageGetter) {121 getter.EXPECT().GetNewMessages(gomock.Any(), "johndoe").Return(nil, errors.New("")).MinTimes(1)122 },123 ExpectedCode: http.StatusInternalServerError,124 ExpectedBody: gin.H{},125 },126 }127 for i, tt := range tests {128 testName := fmt.Sprintf("[%v]", i)129 t.Run(testName, func(t *testing.T) {130 ctrl := gomock.NewController(t)131 defer ctrl.Finish()132 mockedMessageGetter := mocks.NewMockMessageGetter(ctrl)133 if tt.Prepare != nil {134 tt.Prepare(mockedMessageGetter)135 }136 recorder := httptest.NewRecorder()137 _, r := gin.CreateTestContext(recorder)138 r.Use(func(c *gin.Context) {139 c.Set("user", models.User{Username: "johndoe"})140 })141 r.GET("/api/messages/new", GetNewMessages(mockedMessageGetter))142 request, err := http.NewRequest(http.MethodGet, "/api/messages/new", nil)143 if err != nil {144 t.Fatal(err)145 }146 r.ServeHTTP(recorder, request)147 if bodyAssertion, err := common.AreBodiesEqual(tt.ExpectedBody, recorder.Result().Body); err != nil {148 t.Fatal(err)149 } else if !bodyAssertion {150 t.Errorf("response bodies don't match")151 }152 if recorder.Result().StatusCode != tt.ExpectedCode {153 t.Errorf("want %v, got %v", tt.ExpectedCode, recorder.Result().StatusCode)154 }155 })156 }157}158func TestCheckNewMessages(t *testing.T) {159 tests := []struct {160 Prepare func(getter *mocks.MockMessageGetter)161 ExpectedCode int162 ExpectedBody gin.H163 }{164 {165 Prepare: func(getter *mocks.MockMessageGetter) {166 getter.EXPECT().CheckNewMessages(gomock.Any(), "johndoe").Return(5, nil).MinTimes(1)167 },168 ExpectedCode: http.StatusOK,169 ExpectedBody: gin.H{"result": 5},170 }, {171 Prepare: func(getter *mocks.MockMessageGetter) {172 getter.EXPECT().CheckNewMessages(gomock.Any(), "johndoe").Return(0, errors.New("")).MinTimes(1)173 },174 ExpectedCode: http.StatusInternalServerError,175 ExpectedBody: gin.H{},176 },177 }178 for i, tt := range tests {179 testName := fmt.Sprintf("[%v]", i)180 t.Run(testName, func(t *testing.T) {181 ctrl := gomock.NewController(t)182 defer ctrl.Finish()183 mockedMessageGetter := mocks.NewMockMessageGetter(ctrl)184 if tt.Prepare != nil {185 tt.Prepare(mockedMessageGetter)186 }187 recorder := httptest.NewRecorder()188 _, r := gin.CreateTestContext(recorder)189 r.Use(func(c *gin.Context) {190 c.Set("user", models.User{Username: "johndoe"})191 })192 r.GET("/api/messages/check", CheckNewMessages(mockedMessageGetter))193 request, err := http.NewRequest(http.MethodGet, "/api/messages/check", nil)194 if err != nil {195 t.Fatal(err)196 }197 r.ServeHTTP(recorder, request)198 if bodyAssertion, err := common.AreBodiesEqual(tt.ExpectedBody, recorder.Result().Body); err != nil {199 t.Fatal(err)200 } else if !bodyAssertion {201 t.Errorf("response bodies don't match")202 }203 if recorder.Result().StatusCode != tt.ExpectedCode {204 t.Errorf("want %v, got %v", tt.ExpectedCode, recorder.Result().StatusCode)205 }206 })207 }208}209func TestSendMessage(t *testing.T) {210 tests := []struct {211 Body multipart.Form212 Prepare func(sender *mocks.MockMessageSender)213 ExpectedCode int214 ExpectedBody gin.H215 }{216 {217 Body: multipart.Form{218 Value: map[string][]string{219 "to": {"ozkanugur"},220 "body": {"ozkan selam, mazhar ben. bugun biraz asabiydim ama mazaretim var. aksam anlatirim"},221 },222 },223 Prepare: func(sender *mocks.MockMessageSender) {224 sender.EXPECT().SendMessage(gomock.Any(), "ozkan selam, mazhar ben. bugun biraz asabiydim ama mazaretim var. aksam anlatirim", "mazhar", "ozkanugur").Return("", nil).MinTimes(1)225 },226 ExpectedCode: http.StatusCreated,227 ExpectedBody: gin.H{"result": "message is successfully sent"},228 }, {229 Body: multipart.Form{230 Value: map[string][]string{231 "body": {"keske arkadasim olsa da ben de ozkan abi gibi mesajlassam..."},232 },233 },234 Prepare: func(sender *mocks.MockMessageSender) {235 sender.EXPECT().SendMessage(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(0)236 },237 ExpectedCode: http.StatusBadRequest,238 ExpectedBody: gin.H{},239 }, {240 Body: multipart.Form{241 Value: map[string][]string{242 "to": {"fuatguner"},243 },244 },245 Prepare: func(sender *mocks.MockMessageSender) {246 sender.EXPECT().SendMessage(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(0)247 },248 ExpectedCode: http.StatusBadRequest,249 ExpectedBody: gin.H{},250 }, {251 Body: multipart.Form{252 Value: map[string][]string{253 "to": {"tarkan"},254 "body": {"tarkanla mesajlasmak bu kadar kolay miymis yav"},255 },256 },257 Prepare: func(sender *mocks.MockMessageSender) {258 sender.EXPECT().SendMessage(gomock.Any(), "tarkanla mesajlasmak bu kadar kolay miymis yav", "mazhar", "tarkan").Return(259 "",260 services.ErrNoUser,261 ).MinTimes(1)262 },263 ExpectedCode: http.StatusBadRequest,264 ExpectedBody: gin.H{"result": "user does not exist"},265 }, {266 Body: multipart.Form{267 Value: map[string][]string{268 "to": {"tarkan"},269 "body": {"tarkanla mesajlasmak bu kadar kolay miymis yav"},270 }},271 Prepare: func(sender *mocks.MockMessageSender) {272 sender.EXPECT().SendMessage(gomock.Any(), "tarkanla mesajlasmak bu kadar kolay miymis yav", "mazhar", "tarkan").Return("", errors.New("")).MinTimes(1)273 },274 ExpectedCode: http.StatusInternalServerError,275 ExpectedBody: gin.H{},276 },277 }278 for i, tt := range tests {279 testName := fmt.Sprintf("[%v]", i)280 t.Run(testName, func(t *testing.T) {281 ctrl := gomock.NewController(t)282 defer ctrl.Finish()283 mockedMessageSender := mocks.NewMockMessageSender(ctrl)284 if tt.Prepare != nil {285 tt.Prepare(mockedMessageSender)286 }287 recorder := httptest.NewRecorder()288 _, r := gin.CreateTestContext(recorder)289 r.Use(func(c *gin.Context) {290 c.Set("user", models.User{Username: "mazhar"})291 })292 r.POST("/api/messages/send", SendMessage(mockedMessageSender))293 request, err := http.NewRequest(http.MethodPost, "/api/messages/send", nil)294 request.MultipartForm = &tt.Body295 request.Header.Set("Content-Type", "multipart/form-data")296 if err != nil {297 t.Fatal(err)298 }299 r.ServeHTTP(recorder, request)300 if bodyAssertion, err := common.AreBodiesEqual(tt.ExpectedBody, recorder.Result().Body); err != nil {301 t.Fatal(err)302 } else if !bodyAssertion {303 t.Errorf("response bodies don't match")304 }305 if recorder.Result().StatusCode != tt.ExpectedCode {306 t.Errorf("want %v, got %v", tt.ExpectedCode, recorder.Result().StatusCode)307 }308 })309 }310}311func TestReadMessage(t *testing.T) {312 tests := []struct {313 Endpoint string314 Prepare func(reader *mocks.MockMessageReader)315 ExpectedCode int316 ExpectedBody gin.H317 }{318 {319 Endpoint: "/api/messages/read/123456",320 Prepare: func(reader *mocks.MockMessageReader) {321 reader.EXPECT().ReadMessage(gomock.Any(), "123456", "johndoe").Return(nil).MinTimes(1)322 },323 ExpectedCode: http.StatusOK,324 ExpectedBody: gin.H{},325 }, {326 Endpoint: "/api/messages/read/",327 Prepare: func(reader *mocks.MockMessageReader) {328 reader.EXPECT().ReadMessage(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(0)329 },330 ExpectedCode: http.StatusBadRequest,331 ExpectedBody: gin.H{},332 },333 }334 for i, tt := range tests {335 testName := fmt.Sprintf("[%v]", i)336 t.Run(testName, func(t *testing.T) {337 ctrl := gomock.NewController(t)338 defer ctrl.Finish()339 mockedMessageReader := mocks.NewMockMessageReader(ctrl)340 if tt.Prepare != nil {341 tt.Prepare(mockedMessageReader)342 }343 recorder := httptest.NewRecorder()344 _, r := gin.CreateTestContext(recorder)345 r.Use(func(c *gin.Context) {346 c.Set("user", models.User{Username: "johndoe"})347 })348 r.PUT("/api/messages/read/:id", ReadMessage(mockedMessageReader))349 request, err := http.NewRequest(http.MethodPut, tt.Endpoint, nil)350 if err != nil {351 t.Fatal(err)352 }353 r.ServeHTTP(recorder, request)354 if bodyAssertion, err := common.AreBodiesEqual(tt.ExpectedBody, recorder.Result().Body); err != nil {355 t.Fatal(err)356 } else if !bodyAssertion {357 t.Errorf("response bodies don't match")358 }359 if recorder.Result().StatusCode != tt.ExpectedCode {360 t.Errorf("want %v, got %v", tt.ExpectedCode, recorder.Result().StatusCode)361 }362 })363 }364}365func TestReadMessages(t *testing.T) {366 tests := []struct {367 Endpoint string368 Prepare func(reader *mocks.MockMessageReader)369 ExpectedCode int370 ExpectedBody gin.H371 }{372 {373 Endpoint: "/api/messages/user/ali",374 Prepare: func(reader *mocks.MockMessageReader) {375 reader.EXPECT().ReadMessagesFromUser(gomock.Any(), "ali", "johndoe").Return(nil).MinTimes(1)376 },377 ExpectedCode: http.StatusOK,378 ExpectedBody: gin.H{},379 }, {380 Endpoint: "/api/messages/user/",381 Prepare: func(reader *mocks.MockMessageReader) {382 reader.EXPECT().ReadMessagesFromUser(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(0)383 },384 ExpectedCode: http.StatusBadRequest,385 ExpectedBody: gin.H{},386 },387 }388 for i, tt := range tests {389 testName := fmt.Sprintf("[%v]", i)...

Full Screen

Full Screen

CheckLoginAttempt_test.go

Source:CheckLoginAttempt_test.go Github

copy

Full Screen

...29 Login: string(expectedLoginBucket.ID),30 Password: string(expectedPasswordBucket.ID),31 }32 bFactory := mocks.NewMockbucketFactory(gomock.NewController(t))33 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeIp)).Return(&expectedIPBucket, nil).MinTimes(1)34 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypePassword)).Return(&expectedPasswordBucket, nil).MinTimes(1)35 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeLogin)).Return(&expectedLoginBucket, nil).MinTimes(1)36 ipGuard := mocks.NewMockipGuard(gomock.NewController(t))37 ipGuard.EXPECT().HasAccess(useCase.IP).Return(true, nil).MinTimes(1)38 rateLimiter := mocks.NewMockrateLimiter(gomock.NewController(t))39 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedIPBucket)).Return(false, nil).MinTimes(1)40 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedLoginBucket)).Return(false, nil).MinTimes(1)41 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedPasswordBucket)).Return(false, nil).MinTimes(1)42 handler := NewCheckLoginAttemptHandler(bFactory, ipGuard, rateLimiter)43 r := handler.Execute(useCase)44 require.Nil(t, r)45 })46 t.Run("when has not access", func(t *testing.T) { //nolint:dupl47 useCase := CheckLoginAttempt{48 IP: net.ParseIP(string(expectedIPBucket.ID)),49 Login: string(expectedLoginBucket.ID),50 Password: string(expectedPasswordBucket.ID),51 }52 bFactory := mocks.NewMockbucketFactory(gomock.NewController(t))53 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeIp)).Return(&expectedIPBucket, nil).MinTimes(0)54 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypePassword)).Return(&expectedPasswordBucket, nil).MinTimes(0)55 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeLogin)).Return(&expectedLoginBucket, nil).MinTimes(0)56 ipGuard := mocks.NewMockipGuard(gomock.NewController(t))57 ipGuard.EXPECT().HasAccess(useCase.IP).Return(false, nil).MinTimes(1)58 rateLimiter := mocks.NewMockrateLimiter(gomock.NewController(t))59 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedIPBucket)).Return(false, nil).MinTimes(0)60 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedLoginBucket)).Return(false, nil).MinTimes(0)61 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedPasswordBucket)).Return(false, nil).MinTimes(0)62 handler := NewCheckLoginAttemptHandler(bFactory, ipGuard, rateLimiter)63 r := handler.Execute(useCase)64 require.Error(t, r)65 require.Equal(t, r, constants.ErrAccessDenied)66 })67 t.Run("when limited exceeded by ip", func(t *testing.T) { //nolint:dupl68 useCase := CheckLoginAttempt{69 IP: net.ParseIP(string(expectedIPBucket.ID)),70 Login: string(expectedLoginBucket.ID),71 Password: string(expectedPasswordBucket.ID),72 }73 bFactory := mocks.NewMockbucketFactory(gomock.NewController(t))74 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeIp)).Return(&expectedIPBucket, nil).MinTimes(1)75 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypePassword)).Return(&expectedPasswordBucket, nil).MinTimes(1)76 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeLogin)).Return(&expectedLoginBucket, nil).MinTimes(1)77 ipGuard := mocks.NewMockipGuard(gomock.NewController(t))78 ipGuard.EXPECT().HasAccess(useCase.IP).Return(true, nil).MinTimes(1)79 rateLimiter := mocks.NewMockrateLimiter(gomock.NewController(t))80 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedIPBucket)).Return(true, nil).MinTimes(1)81 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedLoginBucket)).Return(false, nil).MinTimes(1)82 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedPasswordBucket)).Return(false, nil).MinTimes(1)83 handler := NewCheckLoginAttemptHandler(bFactory, ipGuard, rateLimiter)84 r := handler.Execute(useCase)85 require.Error(t, r)86 require.Equal(t, r, constants.ErrAttemptsIsExceeded)87 })88 t.Run("when limited exceeded by login", func(t *testing.T) { //nolint:dupl89 useCase := CheckLoginAttempt{90 IP: net.ParseIP(string(expectedIPBucket.ID)),91 Login: string(expectedLoginBucket.ID),92 Password: string(expectedPasswordBucket.ID),93 }94 bFactory := mocks.NewMockbucketFactory(gomock.NewController(t))95 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeIp)).Return(&expectedIPBucket, nil).MinTimes(1)96 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypePassword)).Return(&expectedPasswordBucket, nil).MinTimes(1)97 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeLogin)).Return(&expectedLoginBucket, nil).MinTimes(1)98 ipGuard := mocks.NewMockipGuard(gomock.NewController(t))99 ipGuard.EXPECT().HasAccess(useCase.IP).Return(true, nil).MinTimes(1)100 rateLimiter := mocks.NewMockrateLimiter(gomock.NewController(t))101 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedIPBucket)).Return(false, nil).MinTimes(1)102 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedLoginBucket)).Return(true, nil).MinTimes(1)103 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedPasswordBucket)).Return(false, nil).MinTimes(1)104 handler := NewCheckLoginAttemptHandler(bFactory, ipGuard, rateLimiter)105 r := handler.Execute(useCase)106 require.Error(t, r)107 require.Equal(t, r, constants.ErrAttemptsIsExceeded)108 })109 t.Run("when limited exceeded by password", func(t *testing.T) { //nolint:dupl110 useCase := CheckLoginAttempt{111 IP: net.ParseIP(string(expectedIPBucket.ID)),112 Login: string(expectedLoginBucket.ID),113 Password: string(expectedPasswordBucket.ID),114 }115 bFactory := mocks.NewMockbucketFactory(gomock.NewController(t))116 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeIp)).Return(&expectedIPBucket, nil).MinTimes(1)117 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypePassword)).Return(&expectedPasswordBucket, nil).MinTimes(1)118 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeLogin)).Return(&expectedLoginBucket, nil).MinTimes(1)119 ipGuard := mocks.NewMockipGuard(gomock.NewController(t))120 ipGuard.EXPECT().HasAccess(useCase.IP).Return(true, nil).MinTimes(1)121 rateLimiter := mocks.NewMockrateLimiter(gomock.NewController(t))122 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedIPBucket)).Return(false, nil).MinTimes(1)123 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedLoginBucket)).Return(false, nil).MinTimes(1)124 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedPasswordBucket)).Return(true, nil).MinTimes(1)125 handler := NewCheckLoginAttemptHandler(bFactory, ipGuard, rateLimiter)126 r := handler.Execute(useCase)127 require.Error(t, r)128 require.Equal(t, r, constants.ErrAttemptsIsExceeded)129 })130 t.Run("when limited exceeded by all", func(t *testing.T) { //nolint:dupl131 useCase := CheckLoginAttempt{132 IP: net.ParseIP(string(expectedIPBucket.ID)),133 Login: string(expectedLoginBucket.ID),134 Password: string(expectedPasswordBucket.ID),135 }136 bFactory := mocks.NewMockbucketFactory(gomock.NewController(t))137 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeIp)).Return(&expectedIPBucket, nil).MinTimes(1)138 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypePassword)).Return(&expectedPasswordBucket, nil).MinTimes(1)139 bFactory.EXPECT().Create(gomock.Eq(constants.BucketTypeLogin)).Return(&expectedLoginBucket, nil).MinTimes(1)140 ipGuard := mocks.NewMockipGuard(gomock.NewController(t))141 ipGuard.EXPECT().HasAccess(useCase.IP).Return(true, nil).MinTimes(1)142 rateLimiter := mocks.NewMockrateLimiter(gomock.NewController(t))143 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedIPBucket)).Return(true, nil).MinTimes(1)144 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedLoginBucket)).Return(true, nil).MinTimes(1)145 rateLimiter.EXPECT().IsLimitExceeded(gomock.Eq(&expectedPasswordBucket)).Return(true, nil).MinTimes(1)146 handler := NewCheckLoginAttemptHandler(bFactory, ipGuard, rateLimiter)147 r := handler.Execute(useCase)148 require.Error(t, r)149 require.Equal(t, r, constants.ErrAttemptsIsExceeded)150 })151}...

Full Screen

Full Screen

saver_test.go

Source:saver_test.go Github

copy

Full Screen

...38 ctx, cancelFunc = context.WithCancel(ctx)39 s = saver.NewSaver(ctx, 2, mockAlarm, mockFlusher, saver.CleanOne, "simple alarm imitation")40 s.SaveProject(project)41 s.SaveRepo(repo)42 mockFlusher.EXPECT().FlushProjects(ctx, gomock.Any()).Return(nil).MinTimes(1)43 mockFlusher.EXPECT().FlushRepos(ctx, gomock.Any()).Return(nil).MinTimes(1)44 })45 JustBeforeEach(func() {46 alarms <- struct{}{}47 })48 AfterEach(func() {49 cancelFunc()50 s.Close()51 })52 It("", func() {53 })54 })55 Context("clean all policy", func() {56 var (57 cancelFunc context.CancelFunc58 )59 BeforeEach(func() {60 ctx, cancelFunc = context.WithCancel(ctx)61 s = saver.NewSaver(ctx, 2, mockAlarm, mockFlusher, saver.CleanAll, "clean all policy")62 s.SaveProject(project)63 s.SaveRepo(repo)64 s.SaveProject(project)65 s.SaveRepo(repo)66 s.SaveProject(project)67 s.SaveRepo(repo)68 mockFlusher.EXPECT().FlushRepos(ctx, gomock.Len(1)).Return(nil).Times(1)69 mockFlusher.EXPECT().FlushProjects(ctx, gomock.Len(1)).Return(nil).Times(1)70 mockFlusher.EXPECT().FlushRepos(ctx, gomock.Len(0)).Return(nil).AnyTimes()71 mockFlusher.EXPECT().FlushProjects(ctx, gomock.Len(0)).Return(nil).AnyTimes()72 })73 JustBeforeEach(func() {74 alarms <- struct{}{}75 })76 AfterEach(func() {77 cancelFunc()78 s.Close()79 })80 It("", func() {81 })82 })83 Context("clean one policy", func() {84 var (85 cancelFunc context.CancelFunc86 )87 BeforeEach(func() {88 ctx, cancelFunc = context.WithCancel(ctx)89 s = saver.NewSaver(ctx, 2, mockAlarm, mockFlusher, saver.CleanOne, "clean one policy")90 s.SaveProject(project)91 s.SaveRepo(repo)92 s.SaveProject(project)93 s.SaveRepo(repo)94 s.SaveProject(project)95 s.SaveRepo(repo)96 mockFlusher.EXPECT().FlushRepos(ctx, gomock.Len(2)).Return(nil).Times(1)97 mockFlusher.EXPECT().FlushProjects(ctx, gomock.Len(2)).Return(nil).Times(1)98 mockFlusher.EXPECT().FlushRepos(ctx, gomock.Len(0)).Return(nil).AnyTimes()99 mockFlusher.EXPECT().FlushProjects(ctx, gomock.Len(0)).Return(nil).AnyTimes()100 })101 JustBeforeEach(func() {102 alarms <- struct{}{}103 })104 AfterEach(func() {105 cancelFunc()106 s.Close()107 })108 It("", func() {109 })110 })111 Context("flushing on close without alarm", func() {112 var (113 cancelFunc context.CancelFunc114 )115 BeforeEach(func() {116 ctx, cancelFunc = context.WithCancel(ctx)117 s = saver.NewSaver(ctx, 2, mockAlarm, mockFlusher, saver.CleanOne, "flushing on close without alarm")118 s.SaveProject(project)119 s.SaveRepo(repo)120 mockFlusher.EXPECT().FlushProjects(ctx, gomock.Any()).Return(nil).MinTimes(1)121 mockFlusher.EXPECT().FlushRepos(ctx, gomock.Any()).Return(nil).MinTimes(1)122 })123 JustBeforeEach(func() {124 cancelFunc()125 s.Close()126 })127 It("", func() {128 time.Sleep(time.Second * 2)129 })130 })131 Context("not all flushed", func() {132 var (133 cancelFunc context.CancelFunc134 )135 BeforeEach(func() {136 ctx, cancelFunc = context.WithCancel(ctx)137 s = saver.NewSaver(ctx, 2, mockAlarm, mockFlusher, saver.CleanOne, "not all flushed")138 s.SaveProject(project)139 s.SaveRepo(repo)140 mockFlusher.EXPECT().FlushRepos(ctx, gomock.Any()).Return([]models.Repo{{}}).MinTimes(1)141 mockFlusher.EXPECT().FlushProjects(ctx, gomock.Any()).Return([]models.Project{{}}).MinTimes(1)142 })143 JustBeforeEach(func() {144 alarms <- struct{}{}145 })146 AfterEach(func() {147 cancelFunc()148 s.Close()149 })150 It("", func() {151 })152 })153})...

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1import (2func TestMinTimes(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockInterface(ctrl)6 mock.EXPECT().DoSomething().MinTimes(3)7 mock.DoSomething()8 mock.DoSomething()9 mock.DoSomething()10}11func TestMinTimesError(t *testing.T) {12 ctrl := gomock.NewController(t)13 defer ctrl.Finish()14 mock := NewMockInterface(ctrl)15 mock.EXPECT().DoSomething().MinTimes(3)16 mock.DoSomething()17}18import (19func TestMaxTimes(t *testing.T) {20 ctrl := gomock.NewController(t)21 defer ctrl.Finish()22 mock := NewMockInterface(ctrl)23 mock.EXPECT().DoSomething().MaxTimes(3)24 mock.DoSomething()25 mock.DoSomething()26 mock.DoSomething()27}28func TestMaxTimesError(t *testing.T) {29 ctrl := gomock.NewController(t)30 defer ctrl.Finish()31 mock := NewMockInterface(ctrl)32 mock.EXPECT().DoSomething().MaxTimes(3)33 mock.DoSomething()34 mock.DoSomething()35 mock.DoSomething()36 mock.DoSomething()37}

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1import (2func TestMinTimes(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockFoo(ctrl)6 mock.EXPECT().Bar().MinTimes(2)7}8import (9func TestMaxTimes(t *testing.T) {10 ctrl := gomock.NewController(t)11 defer ctrl.Finish()12 mock := NewMockFoo(ctrl)13 mock.EXPECT().Bar().MaxTimes(2)14}15import (16func TestAnyTimes(t *testing.T) {17 ctrl := gomock.NewController(t)18 defer ctrl.Finish()19 mock := NewMockFoo(ctrl)20 mock.EXPECT().Bar().AnyTimes()21}22import (23func TestTimes(t *testing.T) {24 ctrl := gomock.NewController(t)25 defer ctrl.Finish()26 mock := NewMockFoo(ctrl)27 mock.EXPECT().Bar().Times(2)28}29import (30func TestDo(t *testing.T) {31 ctrl := gomock.NewController(t)32 defer ctrl.Finish()33 mock := NewMockFoo(ctrl)34 mock.EXPECT().Bar().Do(func() {35 fmt.Println("Hello World")36 })37}38import (39func TestAfter(t *testing.T) {40 ctrl := gomock.NewController(t)41 defer ctrl.Finish()42 mock := NewMockFoo(ctrl)43 mock.EXPECT().Bar().After(time.Second)44}

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1import (2func TestMinTimes(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockBar(ctrl)6 mock.EXPECT().DoSomething().MinTimes(1)7 mock.DoSomething()8}9--- PASS: TestMinTimes (0.00s)

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1func TestMinTimes(t *testing.T) {2 mockCtrl := gomock.NewController(t)3 defer mockCtrl.Finish()4 mockObj := NewMockMyInterface(mockCtrl)5 mockObj.EXPECT().MyMethod("foo").MinTimes(1)6 mockObj.MyMethod("foo")7 mockObj.MyMethod("foo")8}9func TestMaxTimes(t *testing.T) {10 mockCtrl := gomock.NewController(t)11 defer mockCtrl.Finish()12 mockObj := NewMockMyInterface(mockCtrl)13 mockObj.EXPECT().MyMethod("foo").MaxTimes(2)14 mockObj.MyMethod("foo")15 mockObj.MyMethod("foo")16}17func TestTimes(t *testing.T) {18 mockCtrl := gomock.NewController(t)19 defer mockCtrl.Finish()20 mockObj := NewMockMyInterface(mockCtrl)21 mockObj.EXPECT().MyMethod("foo").Times(2)22 mockObj.MyMethod("foo")23 mockObj.MyMethod("foo")24}25func TestDo(t *testing.T) {26 mockCtrl := gomock.NewController(t)27 defer mockCtrl.Finish()28 mockObj := NewMockMyInterface(mockCtrl)29 mockObj.EXPECT().MyMethod("foo").Do(func(string) {30 fmt.Println("MyMethod was called")31 })32 mockObj.MyMethod("foo")33}34func TestReturn(t *testing.T) {35 mockCtrl := gomock.NewController(t)36 defer mockCtrl.Finish()37 mockObj := NewMockMyInterface(mock

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1import (2func TestMock(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockCalculator(ctrl)6 mock.EXPECT().Add(1, 2).MinTimes(1)7 mock.Add(1, 2)8 mock.EXPECT().Add(1, 2).MaxTimes(1)9 mock.Add(1, 2)10 mock.EXPECT().Add(1, 2).AnyTimes()11 mock.Add(1, 2)12 mock.EXPECT().Add(1, 2).Times(1)13 mock.Add(1, 2)14}

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1var mock = mock.NewMockSomething(ctrl)2mock.EXPECT().DoSomething().MinTimes(1)3var mock = mock.NewMockSomething(ctrl)4mock.EXPECT().DoSomething().MaxTimes(1)5var mock = mock.NewMockSomething(ctrl)6mock.EXPECT().DoSomething().Times(1)7var mock = mock.NewMockSomething(ctrl)8mock.EXPECT().DoSomething().AnyTimes()9var mock = mock.NewMockSomething(ctrl)10mock.EXPECT().DoSomething().Do(func() {11 fmt.Println("Do something")12})13var mock = mock.NewMockSomething(ctrl)14mock.EXPECT().DoSomething().After(time.Second)15var mock = mock.NewMockSomething(ctrl)16mock.EXPECT().DoSomething().DoAndReturn(func() {17 fmt.Println("Do something")18})19var mock = mock.NewMockSomething(ctrl)20mock.EXPECT().DoSomething().DoAndReturn(func() {21 fmt.Println("Do something")22})23var mock = mock.NewMockSomething(ctrl)24mock.EXPECT().DoSomething().DoAndReturn(func() {25 fmt.Println("Do something")26})27var mock = mock.NewMockSomething(ctrl)28mock.EXPECT().DoSomething().DoAndReturn(func() {29 fmt.Println("Do something")30})31var mock = mock.NewMockSomething(ctrl)32mock.EXPECT().DoSomething().DoAndReturn(func() {33 fmt.Println("Do something")34})

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1import (2func TestMinTimes(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mockObj := mock.NewMockInterface(ctrl)6 mockObj.EXPECT().Method(gomock.Any()).MinTimes(1)7 fmt.Println("min times")8}9import (10func TestMaxTimes(t *testing.T) {11 ctrl := gomock.NewController(t)12 defer ctrl.Finish()13 mockObj := mock.NewMockInterface(ctrl)14 mockObj.EXPECT().Method(gomock.Any()).MaxTimes(1)15 fmt.Println("max times")16}17import (18func TestTimes(t *testing.T) {19 ctrl := gomock.NewController(t)20 defer ctrl.Finish()21 mockObj := mock.NewMockInterface(ctrl)22 mockObj.EXPECT().Method(gomock.Any()).Times(1)23 fmt.Println("times")24}25import (26func TestDo(t *testing.T) {27 ctrl := gomock.NewController(t)28 defer ctrl.Finish()29 mockObj := mock.NewMockInterface(ctrl)30 mockObj.EXPECT().Method(gomock.Any()).Do(func(i int) {31 fmt.Println(i)32 })33 fmt.Println("do")34}35import (36func TestDoAndReturn(t *testing.T) {

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1import (2func TestMinTimes(t *testing.T) {3 controller := gomock.NewController(t)4 mockObj := mock.NewMockMyInterface(controller)5 mockObj.EXPECT().MyFunc().MinTimes(1)6 fmt.Println("Hello World")7}8import (9func TestMaxTimes(t *testing.T) {10 controller := gomock.NewController(t)11 mockObj := mock.NewMockMyInterface(controller)12 mockObj.EXPECT().MyFunc().MaxTimes(2)13 fmt.Println("Hello World")14}15import (16func TestTimes(t *testing.T) {17 controller := gomock.NewController(t)18 mockObj := mock.NewMockMyInterface(controller)19 mockObj.EXPECT().MyFunc().Times(2)20 fmt.Println("Hello World")21}

Full Screen

Full Screen

MinTimes

Using AI Code Generation

copy

Full Screen

1import (2func TestMinTimes(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := _1.NewMockHelloWorld(ctrl)6 mock.EXPECT().HelloWorld(gomock.Any()).MinTimes(1)7 mock.EXPECT().HelloWorld(gomock.Any()).MinTimes(2)8 mock.HelloWorld("Hello")9 mock.HelloWorld("Hello")10 mock.HelloWorld("Hello")11}12import (13func TestMaxTimes(t *testing.T) {14 ctrl := gomock.NewController(t)15 defer ctrl.Finish()16 mock := _2.NewMockHelloWorld(ctrl)17 mock.EXPECT().HelloWorld(gomock.Any()).MaxTimes(1)18 mock.EXPECT().HelloWorld(gomock.Any()).MaxTimes(2)19 mock.HelloWorld("Hello")20 mock.HelloWorld("Hello")21 mock.HelloWorld("Hello")22}23import (24func TestTimes(t *testing.T) {25 ctrl := gomock.NewController(t)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful