Mock should be called exactly 1 times but called 0 times
Looking at your test case:
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()); });}
Either:
- Creating the user calls the
Mail
facade, in which case you're calling that facade before you're mocking it up.
OR
- You aren't calling the function which calls the
Mail
facade.
Either way, Mail::shouldReceive('send')
should not be the last thing in the test case.
The error you are getting is because Mail
is expecting the call to happen after you call ::shouldRecieve()
, but it's not - the test case ends, and the Mockery
instance has never been called.
You could try something like this instead:
public function testSendEmailToNewUserListener(){ $testCase = $this; Mail::shouldReceive('send') ->times(1) ->andReturnUsing(function ($message) use ($testCase) { $testCase->assertEquals('Thank you for registering an account.', $message->getSubject()); $testCase->assertEquals('mcc', $message->getTo()); $testCase->assertEquals(View::make('emails.register'), $message->getBody()); }); $user = factory(MCC\Models\Users\User::class)->create();}
To integrate Mockery with PhpUnit, you just need to define a tearDown() method for your tests containing the following:
public function tearDown() { \Mockery::close();}
This static call cleans up the Mockery container used by the current test, and run any verification tasks needed for your expectations.
You can find more information about Mockery and PhpUnit integation here.