【问题标题】:Unit Testing Mail Queue on Laravel 5Laravel 5 上的单元测试邮件队列
【发布时间】:2016-11-06 13:20:11
【问题描述】:

我正面临两种我想解决/理解的情况。

1st - 如何对 Laravel 的邮件队列类进行单元测试?

我要测试的代码是这样的:

// Create new customer record
$account = $this->create(['account_id' => $account->id]);

// Get email address to send welcome email.
$email = $data['email'];

// Email Subject
$subject = $this->word('emails.welcome.subject');

$this->mailQueue->queue('emails.welcome',
    ['some_data' => 'data'],
    function ($message) use ($email, $subject) {
        $message->to($email)->subject($subject);
    }, true);

return $account;

我想知道在使用Illuminate\Contracts\Mail\MailQueue 类时对我有用的shouldReceive 方法在哪里。

现在我有这个单元测试:

/**
 * @tests
 */
public function it_should_sign_up_a_new_user() {   
    // MailQueue::shouldReceive() does not exist.

    list($account, $email) = $this->getAccountData();
    $request = array_merge($account, $email);
    $account['password'] = $this->hash($account['password']);

    $this->post('/signup', $request, $this->header)
        ->assertResponseOk()
        ->seeInDatabase('account', $account)
        ->seeInDatabase('email', $email);
}

第二个 - 为什么单元测试不需要php artisan queue:listenqueue:work

每次我运行单元测试时,即使我没有运行queue:listen,也会发送电子邮件。我想了解这种神奇的魔法是如何发生的。

【问题讨论】:

标签: php email queue phpunit laravel-5.2


【解决方案1】:

根据文档,他们似乎建议使用 Mail::queue 。即

Mail::queue('emails.welcome', $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

Mail Facade 内置了shouldReceive,因此您应该能够做到:

Mail::shouldReceive('queue')->once()

https://laravel.com/docs/5.0/mail#queueing-mail

【讨论】:

    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 2016-12-08
    • 2018-10-27
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多