我正在尝试对一些触发电子邮件的事件进行单元测试。特别是,我想测试正在发送的电子邮件。
错误是 getTransport() 在模拟对象上不存在。我添加了 shouldReceive('getTransport'),但随后我也必须模拟它返回的内容,然后在返回的模拟对象上调用的函数......等等等等。
我的事件侦听器中的代码:
$data = compact('pusher', 'product', 'time', 'contact');
Mail::send('emails.sms.oos', $data, function($message) {
$message->to('ben@me.com')->subject('');
$message->from('me@newaverfid.com');
});
我的测试:
// 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('me@newaverfid.com');
$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
你可以使模拟成为部分模拟,所以你只是模拟你需要模拟的方法(在你的例子中是 Mail::send() 方法)。对没有定义期望的方法的调用,例如 getTransport() 方法,将在模拟的父"未模拟"类上调用。您可以像这样指定部分模拟:
如果模拟是一个变量:
$mock->makePartial();
直接在立面上指定:
Mail::makePartial();
makePartial() 方法应在定义期望值后调用。