Last active
July 15, 2016 10:10
-
-
Save kavirajk/2f637e250a85e5483ead8840fafac7a7 to your computer and use it in GitHub Desktop.
Send Email unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package email | |
import ( | |
"bytes" | |
"html/template" | |
"reflect" | |
"testing" | |
) | |
type FakeEmailSender struct { | |
to []string | |
from string | |
body []byte | |
} | |
func (f *FakeEmailSender) SendEmail(to []string, from, subject string, body []byte) error { | |
f.to, f.from, f.body = to, from, body | |
return nil | |
} | |
var fakeSender = &FakeEmailSender{} | |
func init() { | |
Sender = fakeSender | |
} | |
func check(t *testing.T, err error) { | |
if err != nil { | |
t.Error(err) | |
} | |
} | |
func TestSendInviteEmail(t *testing.T) { | |
to := []string{"kaviraj@launchyard.com"} | |
ctx := map[string]string{"Email": "new-guy@gmail.com"} | |
err := SendInvitationEmail(to, ctx) | |
check(t, err) | |
if !reflect.DeepEqual(fakeSender.to, to) { | |
t.Errorf("wanted %q, got %q", to, fakeSender.to) | |
} | |
if !reflect.DeepEqual(fakeSender.from, fromEmail) { | |
t.Errorf("wanted %q, got %q", fromEmail, fakeSender.from) | |
} | |
tm := template.Must(template.New("test").Parse(tmplInvite)) | |
var buf bytes.Buffer | |
tm.Execute(&buf, ctx) | |
if !reflect.DeepEqual(fakeSender.body, buf.Bytes()) { | |
t.Errorf("wanted %s, got %s", buf.Bytes(), fakeSender.body) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment