-
Notifications
You must be signed in to change notification settings - Fork 28
/
message_test.go
79 lines (61 loc) · 1.66 KB
/
message_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
package joe
import (
"errors"
"testing"
"github.com/go-joe/joe/reactions"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestMessage_Respond(t *testing.T) {
a := new(MockAdapter)
msg := Message{adapter: a, Channel: "test"}
a.On("Send", "Hello world, The Answer is 42", "test").Return(nil)
msg.Respond("Hello %s, The Answer is %d", "world", 42)
a.AssertExpectations(t)
}
func TestMessage_RespondE(t *testing.T) {
a := new(MockAdapter)
msg := Message{adapter: a, Channel: "test"}
err := errors.New("a wild issue occurred")
a.On("Send", "Hello world", "test").Return(err)
actual := msg.RespondE("Hello world")
assert.Equal(t, err, actual)
a.AssertExpectations(t)
}
func TestMessage_React_NotImplemented(t *testing.T) {
a := new(MockAdapter)
msg := Message{adapter: a}
err := msg.React(reactions.Thumbsup)
assert.Equal(t, ErrNotImplemented, err)
a.AssertExpectations(t)
}
func TestMessage_React(t *testing.T) {
a := new(ExtendedMockAdapter)
msg := Message{adapter: a}
err := errors.New("this clearly failed")
a.On("React", reactions.Thumbsup, msg).Return(err)
actual := msg.React(reactions.Thumbsup)
assert.Equal(t, err, actual)
a.AssertExpectations(t)
}
type MockAdapter struct {
mock.Mock
}
func (a *MockAdapter) RegisterAt(b *Brain) {
a.Called(b)
}
func (a *MockAdapter) Send(text, channel string) error {
args := a.Called(text, channel)
return args.Error(0)
}
func (a *MockAdapter) Close() error {
args := a.Called()
return args.Error(0)
}
type ExtendedMockAdapter struct {
MockAdapter
}
func (a *ExtendedMockAdapter) React(r reactions.Reaction, msg Message) error {
args := a.Called(r, msg)
return args.Error(0)
}