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

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示