Skip to content

Instantly share code, notes, and snippets.

@lakshanwd
Created September 3, 2022 17:09
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