title: Mock should be called exactly 1 times but called 0 times
body: <p>I have weird problem with Laravel 5 and PHPUnit. When I try to mock Laravel's facades (e.g. Auth, View, Mail) I always get this exception:</p>
  <blockquote>
  <p>Mockery\Exception\InvalidCountException: Method send(&quot;emails.register&quot;, array('user'=&gt;'object(MCC\Models\Users\User)',), object(Closure)) from Mockery_0_Illuminate_Mail_Mailer should be called
  exactly 1 times but called 0 times.</p>
  </blockquote>
  <p>I have a problem with <code>&quot;should be called exactly 1 times but called 0 times.&quot;</code> part. This is my test code:</p>
  <pre><code>public function testSendEmailToNewUserListener()
  {
      $user = factory(MCC\Models\Users\User::class)-&gt;create();
  
      Mail::shouldReceive('send')
          -&gt;with(
              'emails.register',
              ['user' =&gt; $user],
              function ($mail) use ($user) {
                 $mail-&gt;to($user-&gt;email, $user-&gt;name)
                      -&gt;subject('Thank you for registering an account.');
              }
          )
          -&gt;times(1)
          -&gt;andReturnUsing(function ($message) use ($user) {
              dd($message);
              $this-&gt;assertEquals('Thank you for registering an account.', $message-&gt;getSubject());
              $this-&gt;assertEquals('mcc', $message-&gt;getTo());
              $this-&gt;assertEquals(View::make('emails.register'), $message-&gt;getBody());
          });
  }
  </code></pre>
  <p>I put <code>dd($message)</code> in closure because I want to know details about return value (how looks <code>$message-&gt;getTo()</code>).</p>
  <p>My TestCase class:</p>
  <pre><code>&lt;?php
  
  /**
   * Provides default values for functional tests.
   *
   * Class TestCase
   */
  abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
  {
      /**
       * The base URL to use while testing the application.
       *
       * @var string
       */
      protected $baseUrl = 'http://004-mcc.dev';
  
      /**
       * Creates the application.
       *
       * @return \Illuminate\Foundation\Application
       */
      public function createApplication()
      {
          $app = require __DIR__ . '/../bootstrap/app.php';
  
          $app-&gt;make(Illuminate\Contracts\Console\Kernel::class)-&gt;bootstrap();
  
          \Illuminate\Support\Facades\Mail::pretend(TRUE);
  
          return $app;
      }
  }
  </code></pre>
  <p>My phpunit.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;phpunit backupGlobals=&quot;false&quot;
       backupStaticAttributes=&quot;false&quot;
       bootstrap=&quot;bootstrap/autoload.php&quot;
       colors=&quot;true&quot;
       convertErrorsToExceptions=&quot;true&quot;
       convertNoticesToExceptions=&quot;true&quot;
       convertWarningsToExceptions=&quot;true&quot;
       processIsolation=&quot;false&quot;
       stopOnFailure=&quot;false&quot;
       syntaxCheck=&quot;false&quot;&gt;
      &lt;testsuites&gt;
          &lt;testsuite name=&quot;Application Test Suite&quot;&gt;
              &lt;directory&gt;./tests/&lt;/directory&gt;
          &lt;/testsuite&gt;
          &lt;testsuite name=&quot;User&quot;&gt;
              &lt;directory&gt;./tests/UserRepository&lt;/directory&gt;
          &lt;/testsuite&gt;
          &lt;testsuite name=&quot;User/Auth&quot;&gt;
              &lt;directory&gt;./tests/UserRepository/Auth&lt;/directory&gt;
          &lt;/testsuite&gt;
          &lt;testsuite name=&quot;User/User&quot;&gt;
              &lt;directory&gt;./tests/UserRepository/User&lt;/directory&gt;
          &lt;/testsuite&gt;
      &lt;/testsuites&gt;
      &lt;filter&gt;
          &lt;whitelist&gt;
              &lt;directory suffix=&quot;.php&quot;&gt;app/&lt;/directory&gt;
          &lt;/whitelist&gt;
      &lt;/filter&gt;
      &lt;php&gt;
          &lt;env name=&quot;APP_ENV&quot; value=&quot;local&quot;/&gt;
          &lt;env name=&quot;CACHE_DRIVER&quot; value=&quot;array&quot;/&gt;
          &lt;env name=&quot;SESSION_DRIVER&quot; value=&quot;array&quot;/&gt;
          &lt;env name=&quot;QUEUE_DRIVER&quot; value=&quot;sync&quot;/&gt;
      &lt;/php&gt;
  &lt;/phpunit&gt;
  </code></pre>
  <p>I checked many sources from Google, from Stackoverflow, most people mentioned about</p>
  <pre><code>$this-&gt;app-&gt;instance('Illuminate\Mail\Mailer', $mockMailer)
  </code></pre>
  <p>but even this instruction doesn't help. Most of questions about this problem are not solved. I checked installed extensions, my Laravel is fresh installed (some models, some routes, about 20 tests).</p>
  <p>Also I tried methods like</p>
  <pre><code>-&gt;atLeast()
  -&gt;times(1)
  </code></pre>
  <p>or</p>
  <pre><code>-&gt;atLeast()
  -&gt;once()
  </code></pre>
  <p>but nothing is works properly. Also instead of</p>
  <pre><code>Mail::shouldReceive('mail')
  </code></pre>
  <p>I used</p>
  <pre><code>$mailMock = Mockery::mock('Illuminate\Mail\Mailer');
  $mailMock-&gt;shouldReceive('mail)
  </code></pre>
  <p>but these methods still not work.</p>
  <p>Rest of console log:</p>
  <pre><code>/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
  </code></pre>
  <p>Also I found one great advice (there <a href="https://stackoverflow.com/a/20531144/4266042">Stackoverflow</a>) but it doesn't work.</p>
  <blockquote>
  <p>Mockery is by default a stubbing library, not a mocking one (which is confusing because of its name).</p>
  <p>That means that -&gt;shouldReceive(...) by default is &quot;zero or more times&quot;. When using -&gt;once(), you say it should be called zero or one time, but not more. This means it'll always pass.</p>
  <p>When you want to assert that it is called once, you can use -&gt;atLeast()-&gt;times(1) (one or more times) or -&gt;times(1) (exactly one time)</p>
  </blockquote>
  <p>My php version: <code>PHP 5.6.14-1+deb.sury.org~trusty+1 (cli)</code></p>
  <p>My apache: <code>Server version: Apache/2.4.16 (Ubuntu)</code></p>
  <p>Mockery version (from composer): <code>&quot;mockery/mockery&quot;: &quot;0.9.*&quot;</code></p>
  <p>Laravel framework (from composer): <code>&quot;laravel/framework&quot;: &quot;5.1.*&quot;</code></p>