注:本文已发布超过一年,请注意您所使用工具的相关版本是否适用
序
笔记
代码实现
Code
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
| package bridge
type IMsgSender interface { Send(msg string) error }
type EmailMsgSender struct { emails []string }
func NewEmailMsgSender(emails []string) *EmailMsgSender { return &EmailMsgSender{emails: emails} }
func (s *EmailMsgSender) Send(msg string) error { return nil }
type INotification interface { Notify(msg string) error }
type ErrorNotification struct { sender IMsgSender }
func NewErrorNotification(sender IMsgSender) *ErrorNotification { return &ErrorNotification{sender: sender} }
func (n *ErrorNotification) Notify(msg string) error { return n.sender.Send(msg) }
|
单元测试
1 2 3 4 5 6 7
| func TestErrorNotification_Notify(t *testing.T) { sender := NewEmailMsgSender([]string{"test@test.com"}) n := NewErrorNotification(sender) err := n.Notify("test msg")
assert.Nil(t, err) }
|
关注我获取更新
猜你喜欢