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条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制