问题描述
有没有一种方法可以在Laravel 5中测试Mail?尝试了我在互联网上看到的唯一合法的Mock示例,但似乎仅适用于Laravel4.下面是当前代码.
Is there a way to test Mail in Laravel 5? tried the only legit Mock example I see on the internet but it seems it only works on Laravel 4. current code below.
$mock = Mockery::mock('Swift_Mailer');
$this->app['mailer']->setSwiftMailer($mock);
...some more codes here...
$mock->shouldReceive('send')->once()
->andReturnUsing(function($msg) {
$this->assertEquals('My subject', $msg->getSubject());
$this->assertEquals('[email protected]', $msg->getTo());
$this->assertContains('Some string', $msg->getBody());
});
这是ApiClient.php的内容,最后一行是第155行,在堆栈跟踪中指示.
this is the contents of ApiClient.php, the last line is line 155, which is indicated in the stack trace.
Mail::queue('emails.error', [
'error_message' => $error_message,
'request' => $request,
'stack_trace' => $stack_trace
], function ($message) use ($error_message) {
$message->to(env('MAIL_TO_EMAIL'), env('MAIL_TO_NAME'))->subject("[Project Error] " . $error_message);
});
下面是堆栈跟踪
Method Mockery_0__vendor_Swift_Mailer::getTransport() does not exist on this mock object
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:285
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:285
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:150
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:255
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php:126
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Queue/Jobs/SyncJob.php:42
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:25
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:184
/Users/BON/WebServer/project/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:216
/Users/BON/WebServer/project/app/Libraries/ApiClient.php:155
/Users/BON/WebServer/project/app/Libraries/ApiClient.php:155
/Users/BON/WebServer/project/app/Libraries/ApiClient.php:174
/Users/BON/WebServer/project/tests/unit_tests/ApiClientUnitTest.php:43
此外,添加use Mockery;
会出现以下错误.
in addition, adding use Mockery;
gets the following error.
PHP Warning: The use statement with non-compound name 'Mockery' has no effect in /Users/BON/WebServer/project/tests/unit_tests/ApiClientUnitTest.php on line 9
这使我沮丧了几个小时,以至于我已经在这里问过.奇怪的是,当Laravel决定升级到版本5时,在进行单元测试时,Laravel没有直接支持测试邮件.
This frustrates me for hours to the point that i'm already asking here on SO. It's just weird that Laravel doesn't have a direct support for testing mails when unittesting when they decided to upgrade to version 5.
推荐答案
让我花了一个下午的大部分时间,但这终于奏效了-我在闭包中传递了一个Mockery对象
Cost me the better part of an afternoon but this is finally what worked - I passed in a Closure and gave it a Mockery object
正在测试的代码:
$subject = "The subject";
Mail::send('emails.emailTemplate', ['user' => $user ],
function( $mail ) use ($user, $subject){
$mail -> to( $user -> email)
-> subject( $subject );
});
有效的测试:
$subject = "The subject";
$user = factory(App\Models\User::class) -> create();
Mail::shouldReceive('send') -> once() -> with(
'emails.emailTemplate',
m::on( function( $data ){
$this -> assertArrayHasKey( 'user', $data );
return true;
}),
m::on( function(\Closure $closure) use ($user, $subject){
$mock = m::mock('Illuminate\Mailer\Message');
$mock -> shouldReceive('to') -> once() -> with( $user -> email )
-> andReturn( $mock ); //simulate the chaining
$mock -> shouldReceive('subject') -> once() -> with($subject);
$closure($mock);
return true;
})
);
这篇关于使用Mock对Laravel 5 Mail进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!