如何使用laravel队列邮件编写单元测试?

时间:2017-08-19 10:28:24

标签: laravel email testing queue tdd

我已经完成了通过队列发送邮件的代码,它工作正常, 我想为它编写一个测试(只是想测试它应该正常发送的邮件,而不是通过队列和正确的人失败),但是如何?

Sub GenerateDates()

    Dim startDate As Date
    Dim endDate As Date
    Dim currentDate As Date
    Dim ws as Worksheet
    Dim iRow As Long

    Set ws = Worksheets("Sheet1") 'or whatever name of sheet you have
    Set wbDesti = Workbooks.Open("C:\Users\...\DestinationFile.xlsx") ' <<< path to source workbook
    Set sh = wbDesti.Worksheets("Sheet1") 'Sheet in destination file

    'automatically find the last row in Sheet A.
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).row + 1

    startDate = ws.cells(1,1).Value
    endDate = ws.cells(2,1).Value

    currentDate = startDate
    'Range("a17").Select 'removed this to avoid .Select functions

    dim row as integer 'declare another variable for row...

    'row = iRow

    'Do Until currentDate = endDate

        'ws.cells(row,1).value = currentDate
        'row = row + 1

        'currentDate = DateAdd("m", 1, currentDate)
        'currentDate = DateSerial(Year(currentDate), Month(currentDate) + 2, 0)

    'Loop
    Dim col as integer
    col = 2 'start with B
    Do until col = 4
        sh.cells(1,col) = application.worksheetfunction.sum(ws.range("B"&10&":"&"B"&":"&60))
        '***other codes goes here to transfer data same as above.
        col = col + 1
    Loop

    wbDesti.quit
    Set sh = Nothing

End Sub

3 个答案:

答案 0 :(得分:4)

你会做这样的事情(取决于你的设置):

<?php

namespace Tests\Feature;

use App\User;
use App\Mail\Welcome;
use Illuminate\Support\Facades\Mail;

class SendInvitationEmailTest extends TestCase
{
    /** @test */
    function mails_get_queued()
    {
        Mail::fake();
        $user = factory(User::class)->create();

        $this->post('/route/to/send/the/welcome/mail');

        Mail::assertQueued(Welcome::class, 1);
        Mail::assertQueued(Welcome::class, function ($mail) use ($user) {
            return $mail->user->id === $user->id;
        });
    }
}

答案 1 :(得分:0)

现在我正在使用MailTracking来测试邮件 https://gist.github.com/anonymous/6e802e56af1f19d53464d667b3e6aa48

答案 2 :(得分:0)

我建议从Mail::queued抓取排队的电子邮件。这是一个简单的数组,可以为您提供所需的全部功能。

赞:

$queuedEmails = Mail::queued(CustomerEmail::class);
$this->assertCount(1, $queuedEmails);
$email = $queuedEmails[0];
$this->assertEquals('status_complete', $email->handle);

您可以按惯常的方式运行断言,以在发生故障时提供更有意义的消息。不幸的是,Mail::assertQueued的失败报告不是很具体或没有帮助:

The expected [App\Mail\MyEmail] mailable was not queued.
Failed asserting that false is true.

也就是说,如果您只是在回调版本中返回true或false。请注意,您可以在回调中使用断言,这很棒,如果您需要检查多个电子邮件,那就更尴尬了。