0
我想單元測試一些事件,觸發電子郵件。特別是,我想測試正在發送的電子郵件。Laravel梅勒 - PHPUnit測試 - 嘲笑不工作
錯誤是getTransport()在模擬對象上不存在。 我添加了shouldReceive('getTransport')但我不得不嘲笑它返回的東西,然後在模擬對象上調用的函數返回...等等等等
我的代碼在我的事件聽衆:
$data = compact('pusher', 'product', 'time', 'contact');
Mail::send('emails.sms.oos', $data, function($message) {
$message->to('[email protected]')->subject('');
$message->from('[email protected]');
});
我的測試:
// Set the expected calls
$mock->shouldReceive('send')
->once()
->with(
'emails.sms.oos',
m::on(function ($data) {
$this->assertArrayHasKey('pusher', $data);
return true;
}),
m::on(function(\Closure $closure) use ($location, $client, $contact, $reader, $pusher){
$message = m::mock('Illuminate\Mailer\Message');
$message->shouldReceive('to')->once()->with($contact->cellEmail())
->andReturn($message);
$message->shouldReceive('subject')->once()->with('');
$message->shouldReceive('from')->once()->with('[email protected]');
$closure($message);
return true;
})
);
而且......我的錯誤:
1) InventoryEventsTest::it_only_emails_contacts_for_the_specific_location
BadMethodCallException: Method Mockery_1_Swift_Mailer::getTransport() does not exist on this mock object
C:\xampp\htdocs\dev\newdash\vendor\laravel\framework\src\Illuminate\Mail\Mailer.php:315
C:\xampp\htdocs\dev\newdash\vendor\laravel\framework\src\Illuminate\Mail\Mailer.php:159
C:\xampp\htdocs\dev\newdash\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:222
C:\xampp\htdocs\dev\newdash\app\Listeners\SMSInventoryOutOfStockNotification.php:47
C:\xampp\htdocs\dev\newdash\app\Listeners\SMSInventoryOutOfStockNotification.php:47
C:\xampp\htdocs\dev\newdash\tests\Integrated\InventoryEventsTest.php:157
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
當我這樣做時,我得到一個新的錯誤**致命錯誤:調用成員函數stop()在C:\ xampp \ htdocs \ dev \ newdash \ vendor \ laravel \ framework \ src中的非對象\ Illuminate \ Mail \ Mailer.php on line 315 ** – KiaiFighter
然後你的期望一定是不正確的(with() - 方法中的參數與實際發送的參數不匹配),所以它使用真正的send()方法,而不是你的嘲笑。如果你改變模擬爲'$ mock-> shouldReceive('send') - > once();',這個工作嗎? –
是的,它只與' - > shouldReceive('send') - > once()'一起工作,但不是所有其他方法...猜我會玩更多...... – KiaiFighter