dongmou2389 2015-04-21 19:39
浏览 39
已采纳

PHPUnit Mocked方法导致异常

I have this function in a class

public function doSomething()
{
    try {
        $result = functionCall();
        if ($result === true) {
            $this->doGoodResult('With Success Message');
        } else {
            $this->doBadResult('ERROR - unable to do something');
        }
    } catch (Exception $e) {
        $this->doBadResult('Did you divide by zero?');
    }
}

and I'm trying to test it with mocking out the extra functions

public function test_doSomthingWithBadResultGetsDoBadResultCalled()
{
    $ajc = $this->getMockBuilder('MyClass')
        ->setMethods(array('doBadResult'))
        ->setConstructorArgs(array('doSomething', array('input_var' => 0)))
        ->getMock();
    $ajc->expects($this->once())
        ->method('doBadResult')
        ->willReturn(null)
        ->with($this->contains('ERROR - unable to do something'));
    $ajc->doSomething();
}

and the problem is that I'm getting the following error from PHPUnit

MyClass::doBadResult('Did you divide by zero?') was not expected to be called more than once.

I'm not sure why, but it looks like the catch statement is being executed only when in this mock scenario.

Could anyone help me figure out why this is happening and especially how to set this up so that my test doesn't fail?

  • 写回答

2条回答 默认 最新

  • duanshan1977 2015-04-21 20:38
    关注

    After some debugging I found the Exception was Invalid argument supplied for foreach() and after some digging, it was in the $this->contains('ERROR...')

    the answer is $this->contains should be $this->stringContains

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度