Skip to content

Commit

Permalink
[#30] test: fcm 토큰 수정 기능 관련 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
YehyeokBang committed Jan 29, 2024
1 parent 836b960 commit 545101b
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions app/user/services/user_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,123 @@ func TestUserService_Login(t *testing.T) {
assert.NotEmpty(t, responseToken.Token)
}

func TestUserService_UpdateFcmToken(t *testing.T) {
// Mock UserRepository and UserUtil
mockRepo := new(MockUserRepository)
mockUtil := new(MockUserUtil)

// Set up sample user for the test
input := types.RequestUpdateFcmToken{
FcmToken: "new_token",
}

// Set up expectations
currentUser := &models.User{
ID: 1,
Name: "test",
Nickname: "test",
Email: "test@gmail.com",
Age: 25,
Gender: "male",
FcmToken: "old_token",
}

updatedUser := &models.User{
ID: 1,
Name: "test",
Nickname: "test",
Email: "test@gmail.com",
Age: 25,
Gender: "male",
FcmToken: "new_token",
}

// Set up expectations for the mock util
mockUtil.On("FindCurrentUser", mock.AnythingOfType("*gin.Context")).Return(currentUser, nil)

// Set up expectations for the mock repository
mockRepo.On("Update", mock.AnythingOfType("*models.User")).Return(*updatedUser, nil)

// Create UserService with the mock repository and util
userService := services.NewUserService(mockRepo, mockUtil)

// Create a test context
ctx, _ := gin.CreateTestContext(nil)

// Call the method under test
err := userService.UpdateFcmToken(ctx, input)

// Assert that the expectations were met
mockUtil.AssertExpectations(t)
mockRepo.AssertExpectations(t)

// Check the results
assert.Nil(t, err)
}

func TestUserService_UpdateFcmToken_NoUser(t *testing.T) {
// Mock UserRepository
mockRepo := new(MockUserRepository)
mockUtil := new(MockUserUtil)

// Set up expectations for the mock util
mockUtil.On("FindCurrentUser", mock.AnythingOfType("*gin.Context")).Return((*models.User)(nil), errors.New("user not found"))

// Create UserService with the mock repository and util
userService := services.NewUserService(mockRepo, mockUtil)

// Create a test context
ctx, _ := gin.CreateTestContext(nil)

// Set up sample user for the test
input := types.RequestUpdateFcmToken{
FcmToken: "new_token",
}

// Call the method under test
err := userService.UpdateFcmToken(ctx, input)

// Assert that the expectations were met
mockUtil.AssertExpectations(t)

// Check the results
assert.NotNil(t, err)
assert.EqualError(t, err, "user not found")
}

func TestUserService_UpdateFcmToken_Error(t *testing.T) {
// Mock UserRepository
mockRepo := new(MockUserRepository)
mockUtil := new(MockUserUtil)

// Set up expectations for the mock util
mockUtil.On("FindCurrentUser", mock.AnythingOfType("*gin.Context")).Return(&models.User{}, nil)

// Set up sample user for the test
input := types.RequestUpdateFcmToken{
FcmToken: "new_token",
}

// Set up expectations for the mock repository
mockRepo.On("Update", mock.AnythingOfType("*models.User")).Return(models.User{}, errors.New("record not found"))

// Create UserService with the mock repository and util
userService := services.NewUserService(mockRepo, mockUtil)

// Create a test context
ctx, _ := gin.CreateTestContext(nil)

// Call the method under test
err := userService.UpdateFcmToken(ctx, input)

// Assert that the expectations were met
mockUtil.AssertExpectations(t)
mockRepo.AssertExpectations(t)

// Check the results
assert.NotNil(t, err)
}

func TestUserService_GetUserInfo(t *testing.T) {
// Mock UserRepository
mockRepo := new(MockUserRepository)
Expand Down

0 comments on commit 545101b

Please sign in to comment.