-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathslack_test.go
82 lines (72 loc) · 1.97 KB
/
slack_test.go
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
package slack
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestSlack_Send(t *testing.T) {
t.Parallel()
tests := []struct {
name string
channelIDs []string
subject string
message string
mockSetup func(*mockslackClient)
expectedError string
}{
{
name: "Successful send to single channel",
channelIDs: []string{"C1234567890"},
subject: "Test Subject",
message: "Test Message",
mockSetup: func(m *mockslackClient) {
m.On("PostMessageContext", mock.Anything, "C1234567890", mock.AnythingOfType("slack.MsgOption")).
Return("", "", nil)
},
expectedError: "",
},
{
name: "Successful send to multiple channels",
channelIDs: []string{"C1234567890", "C0987654321"},
subject: "Test Subject",
message: "Test Message",
mockSetup: func(m *mockslackClient) {
m.On("PostMessageContext", mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("slack.MsgOption")).
Return("", "", nil).
Twice()
},
expectedError: "",
},
{
name: "Slack client error",
channelIDs: []string{"C1234567890"},
subject: "Test Subject",
message: "Test Message",
mockSetup: func(m *mockslackClient) {
m.On("PostMessageContext", mock.Anything, "C1234567890", mock.AnythingOfType("slack.MsgOption")).
Return("", "", errors.New("Slack error"))
},
expectedError: "send message to channel \"C1234567890\": Slack error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mockClient := new(mockslackClient)
tt.mockSetup(mockClient)
s := &Slack{
client: mockClient,
channelIDs: tt.channelIDs,
}
err := s.Send(context.Background(), tt.subject, tt.message)
if tt.expectedError != "" {
require.EqualError(t, err, tt.expectedError)
} else {
require.NoError(t, err)
}
mockClient.AssertExpectations(t)
})
}
}