作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望在这个类中测试方法:
class EmailerService
{
protected $mailer;
protected $router;
protected $em;
protected $emailMan;
protected $emailReminderMan;
protected $secret;
/**
* Construct
*
* @param \Swift_Mailer $mailer
* @param \Symfony\Bundle\FrameworkBundle\Routing\Router $router
* @param \Doctrine\ORM\EntityManager $em
* @param EmailManager $emailMan
* @param EmailReminderManager $emailReminderMan
* @param $secret
*/
public function __construct(Swift_Mailer $mailer, Router $router, EntityManager $em, EmailManager $emailMan, EmailReminderManager $emailReminderMan, $secret)
{
$this->mailer = $mailer;
$this->router = $router;
$this->em = $em;
$this->emailMan = $emailMan;
$this->emailReminderMan = $emailReminderMan;
$this->secret = $secret;
}
class EmailerServiceTest extends \PHPUnit_Framework_TestCase
{
protected $emailer;
public function setUp()
{
$mailer = $this->getMockBuilder('Swift_Mailer')
->disableOriginalConstructor()
->getMock();
$router = $this->getMockBuilder('Router')
->disableOriginalConstructor()
->getMock();
$em = $this->getMockBuilder('EntityManager')
->disableOriginalConstructor()
->getMock();
$emailMan = $this->getMockBuilder('EmailManager')
->disableOriginalConstructor()
->getMock();
$emailReminderMan = $this->getMockBuilder('EmailReminderManager')
->disableOriginalConstructor()
->getMock();
$secret = '123';
$this->emailer = new EmailerService($mailer, $router, $em, $emailMan, $emailReminderMan, $secret);
}
1) T\CBundle\Tests\Service\EmailerServiceTest::testGetVariablesForModule Argument 2 passed to T\CBundle\Service\EmailerService::__construct() must be an instance of Symfony\Bundle\FrameworkBundle\Routing\Router, instance of Mock_Router_3e61717e given, called in ...
最佳答案
你必须使用真正的类名,否则 PHPunit 将创建一个名为 Router
的类。 (注意:这不是预期的 Symfony\Component\Routing\Router
):
// don't need it here, Swift_Mailer is in the global scope
$mailer = $this->getMockBuilder('Swift_Mailer')
->disableOriginalConstructor()
->getMock();
$router = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\Router')
->disableOriginalConstructor()
->getMock();
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$emailMan = $this->getMockBuilder('Full\Namespace\To\EmailManager')
->disableOriginalConstructor()
->getMock();
$emailReminderMan = $this->getMockBuilder('Full\Namespace\To\EmailReminderManager')
->disableOriginalConstructor()
->getMock();
关于symfony - 如何用phpunit模拟依赖注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21476638/
我是一名优秀的程序员,十分优秀!