【问题标题】:Expectation in phpunit test casephpunit 测试用例中的期望
【发布时间】:2015-09-18 21:56:55
【问题描述】:

我有索引方法

public function indexAction()
{

       $em = $this->getDoctrine()->getManager();

       $entities = $em->getRepository('AppBundle:Blog')->findAll();

       return array(
           'entities' => $entities,
       );
   }  // End of method 

当我尝试对此操作进行单元测试时,如下所示

  public function testIndexAction()
   {

       $entityManager = $this->getMockBuilder('BlogController')
           ->disableOriginalConstructor()
           ->setMethods(array('getRepository'))
           ->getMock();

       $entityManager->expects($this->once())
           ->method('findAll')
           ->will($this->returnValue(0));    
   }  // End of method 

它在下面给我错误

AppBundle\Tests\Controller\BlogController\BlogControllerTest::testIndexAction 方法名称的期望失败等于何时 调用了 1 次。

方法预期调用1次,实际调用0次。

【问题讨论】:

  • findAll 在存储库对象而不是实体管理器上被调用。两个完全不同的对象。

标签: symfony phpunit


【解决方案1】:

$entityManager 被构建为 BlogController 的模拟(在全局命名空间中)没有意义。

所以BlogController::findAll 不被调用是完全合乎逻辑的。

因此,您的测试只是创建模拟,而不是对它们执行任何操作。所以没有理由调用任何方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多