PHPUnit:如何定义存根的错误消息预计失败?

时间:2011-12-07 21:41:40

标签: phpunit

我的测试方法只验证模拟方法是否被调用两次。 当它失败时,我想向用户提供错误消息。

我该怎么办?

代码示例:

public function testUpdate()
{
    $emMock = $this->mockEntityManager(
        array('persist', 'flush'),
        array('name')
    );
    $srv = new Service($emMock);

    $entity = $srv->create();

    $emMock
        ->expects($this->exactly(2))
        ->method('persist');
    $emMock
        ->expects($this->exactly(3)) //Should give an error message
        ->method('flush');

    $srv->update($entity);
}

1 个答案:

答案 0 :(得分:0)

失败时抛出异常

$emMock->expects($this->exactly(3))
       ->method('persist')
       ->will($this->throwException(new Exception('Called too many times.')));