Testing mail
@fake_mail() keeps every message the test sends from then on, queued or not, instead of sending it. Its methods say what was sent:
<import package="KScript.Test" />
<import package="KScript.Mail" />
<test name="sign-up sends a welcome email">
<let name="mail" value="@fake_mail()" />
<do>@send_mail(to: 'joan@example.com', subject: 'Welcome', text: 'Hi Joan', queue)</do>
<expect value="$mail->has_sent('joan@example.com')" equals="true" />
<expect value="$mail->sent('joan@example.com')[0].subject" equals="'Welcome'" />
<expect value="$mail->queued()->count()" equals="1" />
</test>import KScript.Test
import KScript.Mail
test(name: 'sign-up sends a welcome email') {
$mail = @fake_mail()
@send_mail(to: 'joan@example.com', subject: 'Welcome', text: 'Hi Joan', queue)
expect($mail->has_sent('joan@example.com'), equals: true)
expect($mail->sent('joan@example.com')[0].subject, equals: 'Welcome')
expect($mail->queued()->count(), equals: 1)
}| Method | What it gives |
|---|---|
->sent(to?) |
The messages, or only those to one address or user: maps of to, cc, bcc, from, reply_to, subject, html, text, view, attachments and is_queued. |
->queued(to?) |
The messages sent with queue. |
->has_sent(to) |
True when mail went to the address, as to, cc or bcc. |
->count() |
How many messages were sent. |
->count_of(to) |
How many went to one address. |
->clear() |
Forgets them, and returns how many there were. |
@fake_mail() works in routes a test calls too, so a test can post a sign-up form and then check the email it sent.