jeudi 7 mai 2015

Mockery mock method inside closure

I have problem unit testing method inside closure called by call_user_func() example :

public function trans($lang, $callback)
{
   $this->sitepress->switch_lang($lang);
   call_user_func($callback);
}

on controller :

public function sendMail()
{
   $foo = $baz = 'something';
   $mail = $this->mailer;
   $this->helper->trans_c('en', function() use($foo, $baz, $mail) {
      $mail->send('Subject', $foo, $baz);
   });
}

test case :

public function testSomething()
{
   $helperMock = Mockery::mock('Acme\Helper');
   $helperMock->shouldReceive('trans_c')->once(); // passed

   $mailMock = Mockery::mock('Acme\Mail');
   $mailMock->shouldReceive('send')->once(); // got should be called 1 times instead 0

   $act = new SendMailController($helperMock, $mailMock);
   $act->sendMail();
}

how can I ensure that ->send() method is called inside closure trans_c()

I tried with

$helperMock->shouldReceive('trans_c')->with('en', function() use($mailMock) {
   $mailMock->shouldReceive('send');
});

no luck. :(

well it works fine with passing Mockery::type('Closure') in the second param of trans_c, but I really need to ensure that method send from mailer class is called.

Aucun commentaire:

Enregistrer un commentaire