Created
September 3, 2022 17:09
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
import ( | |
"testing" | |
"testing-demo/dependency" | |
"github.com/golang/mock/gomock" | |
"github.com/stretchr/testify/assert" | |
) | |
func Test_Notification_Service(t *testing.T) { | |
ctrl := gomock.NewController(t) | |
defer ctrl.Finish() | |
t.Run("SendTextNotification", func(t *testing.T) { | |
mockSmsSvc := dependency.NewMockSmsProvider(ctrl) | |
notifyClient := NewNotificationClient(mockSmsSvc, nil) | |
t.Run("with invalid phone number", func(t *testing.T) { | |
number, message := "1233452345", "message" | |
messageID, err := notifyClient.SendTextNotification(number, message) | |
assert.Empty(t, messageID) | |
assert.NotNil(t, err) | |
}) | |
t.Run("with valid phone number", func(t *testing.T) { | |
number, message := "012 234 2345", "message" | |
mockSmsSvc.EXPECT().SendSMS(number, message).Return("message-id", nil) | |
messageID, err := notifyClient.SendTextNotification(number, message) | |
assert.NotEmpty(t, messageID) | |
assert.Nil(t, err) | |
}) | |
}) | |
t.Run("SendEmailNotification", func(t *testing.T) { | |
mockEmailSvc := dependency.NewMockEmailProvider(ctrl) | |
notifyClient := NewNotificationClient(nil, mockEmailSvc) | |
t.Run("with invalid email address", func(t *testing.T) { | |
from, to, subject, body := "from", "to", "subject", "body" | |
messageID, err := notifyClient.SendEmailNotification(from, to, subject, body) | |
assert.Empty(t, messageID) | |
assert.NotNil(t, err) | |
}) | |
t.Run("with valid email address", func(t *testing.T) { | |
from, to, subject, body := "from", "johndoe@example.com", "subject", "body" | |
mockEmailSvc.EXPECT().SendEmail(from, to, subject, body).Return("message-id", nil) | |
messageID, err := notifyClient.SendEmailNotification(from, to, subject, body) | |
assert.NotEmpty(t, messageID) | |
assert.Nil(t, err) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment