最近我在我的项目上看到了一些问题,发现有一些例外,我忘了抓住它们!这是我的代码:
try {
$this->user = $invoiceEvent->user;
$this->invoice = $invoiceEvent->invoice;
if ($this->user->email) {
$this->sendEmail();
}
}catch (Swift_RfcComplianceException $e) {
}
通过使用这个try / catch我的问题解决了,这是我的测试,并且是绿色的!,谁可以断言异常是catch?
/**
* @test
*/
public function it_should_Not_provide_exception_when_mailFromAddress_is_not_set()
{
$invoice = $this->makeInvoice();
$user = $this->makeUser();
$mail = app('Illuminate\Contracts\Mail\Mailer');
(new SendInvoiceToUser($mail))->handle(new InvoiceCreated($invoice, $user));
}
答案 0 :(得分:0)
最后使用这种方式我断言异常捕获我的方法在这种情况下返回null,这正是我除了。
/**
* @test
*/
public function it_should_Not_provide_exception_when_mailFromAddress_is_not_set()
{
$invoice = $this->makeInvoice();
$user = $this->makeUser();
$mail = app('Illuminate\Contracts\Mail\Mailer');
$mocked = $this->getMockBuilder(SendInvoiceToUser::class)
->setConstructorArgs([$mail])
->setMethods(null)
->getMock();
$this->assertNull($mocked->handle(new InvoiceCreated($invoice, $user)));
}