I have a strange problem with Laravel 5 and PHPUnit. When I try to make fun of Laravel facades (e.g. Auth, View, Mail), I always get this exception:
Mockery \ Exception \ InvalidCountException: the send ("emails.register", array ('user' => 'object (MCC \ Models \ Users \ User)',), object (Closure)) method from Mockery_0_Illuminate_Mail_Mailer should be called exactly 1 time, but called 0 times.
I have a problem with the part "should be called exactly 1 times but called 0 times."
. This is my test code:
public function testSendEmailToNewUserListener()
{
$user = factory(MCC\Models\Users\User::class)->create();
Mail::shouldReceive('send')
->with(
'emails.register',
['user' => $user],
function ($mail) use ($user) {
$mail->to($user->email, $user->name)
->subject('Thank you for registering an account.');
}
)
->times(1)
->andReturnUsing(function ($message) use ($user) {
dd($message);
$this->assertEquals('Thank you for registering an account.', $message->getSubject());
$this->assertEquals('mcc', $message->getTo());
$this->assertEquals(View::make('emails.register'), $message->getBody());
});
}
I put dd($message)
in a close because I want to know the details of the return value (what it looks like $message->getTo()
).
My TestCase class:
<?php
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
protected $baseUrl = 'http://004-mcc.dev';
public function createApplication()
{
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
\Illuminate\Support\Facades\Mail::pretend(TRUE);
return $app;
}
}
My phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
<testsuite name="User">
<directory>./tests/UserRepository</directory>
</testsuite>
<testsuite name="User/Auth">
<directory>./tests/UserRepository/Auth</directory>
</testsuite>
<testsuite name="User/User">
<directory>./tests/UserRepository/User</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="local"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
I checked a lot of sources from Google, from Stackoverflow, most people mentioned
$this->app->instance('Illuminate\Mail\Mailer', $mockMailer)
. . , Laravel ( , , 20 ).
,
->atLeast()
->times(1)
->atLeast()
->once()
.
Mail::shouldReceive('mail')
$mailMock = Mockery::mock('Illuminate\Mail\Mailer');
$mailMock->shouldReceive('mail)
.
:
/home/grzgajda/programowanie/php/005mcc/vendor/mockery/mockery/library/Mockery/CountValidator/Exact.php:37
/home/grzgajda/programowanie/php/005mcc/vendor/mockery/mockery/library/Mockery/Expectation.php:271
/home/grzgajda/programowanie/php/005mcc/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:120
/home/grzgajda/programowanie/php/005mcc/vendor/mockery/mockery/library/Mockery/Container.php:297
/home/grzgajda/programowanie/php/005mcc/vendor/mockery/mockery/library/Mockery/Container.php:282
/home/grzgajda/programowanie/php/005mcc/vendor/mockery/mockery/library/Mockery.php:142
/home/grzgajda/programowanie/php/005mcc/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:48
/home/grzgajda/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:148
/home/grzgajda/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:100
( qaru.site/questions/1516764/...), .
Mockery stubbing, ( - ).
, → shouldReceive (...) - " ". → once() , , . , .
, , → atLeast() → times (1) ( ) → times (1) ( )
php: PHP 5.6.14-1+deb.sury.org~trusty+1 (cli)
apache: Server version: Apache/2.4.16 (Ubuntu)
mockery ( ): "mockery/mockery": "0.9.*"
Laravel framework ( ): "laravel/framework": "5.1.*"