【发布时间】:2015-07-17 16:34:21
【问题描述】:
call_user_func() 示例调用的闭包内的单元测试方法有问题:
public function trans($lang, $callback)
{
$this->sitepress->switch_lang($lang);
call_user_func($callback);
}
在控制器上:
public function sendMail()
{
$foo = $baz = 'something';
$mail = $this->mailer;
$this->helper->trans_c('en', function() use($foo, $baz, $mail) {
$mail->send('Subject', $foo, $baz);
});
}
测试用例:
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();
}
我怎样才能确保->send()方法在闭包trans_c()中被调用
我试过了
$helperMock->shouldReceive('trans_c')->with('en', function() use($mailMock) {
$mailMock->shouldReceive('send');
});
运气不好。 :(
在trans_c 的第二个参数中传递Mockery::type('Closure') 可以正常工作,但我确实需要确保调用邮件类中的send 方法。
【问题讨论】:
-
请发布您遇到的错误。没有运气对我们没有帮助。
标签: unit-testing laravel-4 mocking phpunit mockery