2016-07-04 52 views
1

我面臨兩個我想解決/理解的情況。Laravel 5上的單元測試郵件隊列

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; 

我想知道哪裏是shouldReceive方法將使用Illuminate\Contracts\Mail\MailQueue類時爲我工作。

現在我對這個這個單元測試:

/** 
* @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); 
} 

2日 - 爲什麼單元測試不需要php artisan queue:listenqueue:work

每次運行單元測試時,即使我沒有運行queue:listen,也會分派電子郵件。我想了解這個令人敬畏的魔法是如何發生的。

+0

[使用模擬單元測試Laravel 5郵件]的可能的複製(HTTP ://stackoverflow.com/questions/31120567/unittesting-laravel-5-mail-using-mock) –

回答

1

根據文件,看起來好像他們推薦使用Mail::queue。即

Mail::queue('emails.welcome', $data, function($message) 
{ 
    $message->to('[email protected]', 'John Smith')->subject('Welcome!'); 
}); 

的郵件門面已shouldReceive內置到它,所以你應該能夠做到:

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

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