-
Notifications
You must be signed in to change notification settings - Fork 1
/
publisher_test.go
57 lines (47 loc) · 1.14 KB
/
publisher_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
package peanats
import (
"testing"
"github.com/nats-io/nats.go"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type publisherMock struct {
mock.Mock
}
func (p *publisherMock) Subject() string {
args := p.Called()
return args.String(0)
}
func (p *publisherMock) WithSubject(subj string) Publisher {
args := p.Called(subj)
return args.Get(0).(Publisher)
}
func (p *publisherMock) Header() *nats.Header {
args := p.Called()
return args.Get(0).(*nats.Header)
}
func (p *publisherMock) Publish(data []byte) error {
args := p.Called(data)
return args.Error(0)
}
func (p *publisherMock) PublishMsg(msg *nats.Msg) error {
args := p.Called(msg)
return args.Error(0)
}
func TestPublisher(t *testing.T) {
m := publisherMock{}
m.On("PublishMsg", &nats.Msg{
Subject: "the.parson.had.a.dog",
Header: nats.Header{
"the-parson": []string{"had a dog"},
},
Data: []byte("the parson had a dog"),
}).Return(nil)
p := publisherImpl{
PublisherMsg: &m,
subject: "the.parson.had.a.dog",
}
p.Header().Add("the-parson", "had a dog")
err := p.Publish([]byte("the parson had a dog"))
require.NoError(t, err)
}