dongzhuo1930 2016-12-21 11:50
浏览 22
已采纳

PHPUnit样机问题

I have a mockup of a service class. It's defined like this in my setUp function

$this->myServiceMockup = $this->getMockBuilder(MyService::class)
            ->disableOriginalConstructor()
            ->setMethods(['myMethod'])
            ->getMock();

In my test function i set an expectation for it like this.

$this->myServiceMockup->expects($this->once())
            ->method('myMethod')
            ->with($this->exactly(1), 'myName')
            ->willReturn($this->exactly(1));

So what this means is that when i should trigger the myMethod function only once and that it will return integer 1.

So method that I'm testing has that line of code.

$myIntValue = $this->myService->myMethod($number, $name);

After this line $myIntValue when i run the test should be 1 and test should continue, well that's my understanding on this.

But instead I get this error

Expectation failed for method name is equal to when invoked 1 time(s) Parameter 0 for invocation

My\Path\To\Class\MyService::myMethod(1, 'myName') does not match expected value.

1 does not match expected type "object".

Doesn't make any sense since myMethod is expecting an integer and a string.

public function myMethod($number, $name)
{
    return $this->table->save($number, $name);
}

Can somebody explain to me what am I doing wrong here, because I'm out of ideas.

  • 写回答

2条回答 默认 最新

  • duanliaolan6178 2016-12-21 11:55
    关注

    The exactly() is an invocation count matcher (like once() or any()) for using as argument of the expects() method.

    Just replace the:

    ->with($this->exactly(1), 'myName')
    

    to

    ->with(1, 'myName')
    

    The willReturn() also accepts values "as is".

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统