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

发布于 2024-12-20 02:55:20 字数 548 浏览 2 评论 0原文

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

我怎样才能这样做呢?

代码示例:

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);
}

I've test method that only validate if a mock method is called twice.
When it fail, I would like to provide the user with an error message.

How can I do so ?

Code sample:

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2024-12-27 02:55:20

失败时抛出异常

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

Throw an exception when failure

$emMock->expects($this->exactly(3))
       ->method('persist')
       ->will($this->throwException(new Exception('Called too many times.')));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文