mailgun/mailgun-go

Make properties on Message struct publicly accessible

robinvdvleuten opened this issue · 2 comments

Is there a reason that all properties on the Message struct are not publicly accessible? It would be great to have them accessible for testing purposes;

type MailgunMock struct {
	mailgunImpl *mailgun.MailgunImpl
	messages    []*mailgun.Message
}

func NewMailgunMock() *MailgunMock {
	return &MailgunMock{
		mailgunImpl: &mailgun.MailgunImpl{},
		messages:    []*mailgun.Message{},
	}
}

func (m *MailgunMock) Send(ctx context.Context, msg *mailgun.Message) (string, string, error) {
	m.messages = append(m.messages, msg)
	return "", "", nil
}

func (m *MailgunMock) NewMessage(from, subject, text string, to ...string) *mailgun.Message {
	return m.mailgunImpl.NewMessage(from, subject, text, to...)
}

And then verify them in a test case

mg := NewMailgunMock()

// Call logic which uses Mailgun mock

// Verify that a message is send to Mailgun
assert.Len(t, mg.messages, 1)

// Verify that a message is send with correct subject (currently not possible).
assert.Equal(t, mg.messages[0].Subject, "Foo")

I don't see a Subject field on the Message struct. You can get the Subject from the GetHeaders() method. We are following the rules of encapsulation which why those fields are private. https://ardalis.com/encapsulation-in-objects-and-applications/

@thrawn01 Could we add getters for these fields? This would still maintain encapsulation, while providing a simple solution for a problem that multiple people are apparently facing, based on other similar issues I've seen.