PHPUnit实体模型问题

PHPUnit实体模型问题,php,mocking,phpunit,Php,Mocking,Phpunit,我有一个服务类的模型。 在我的设置函数中定义如下 $this->myServiceMockup = $this->getMockBuilder(MyService::class) ->disableOriginalConstructor() ->setMethods(['myMethod']) ->getMock(); 在我的测试函数中,我设置了这样的期望值 $this->myServi

我有一个服务类的模型。 在我的
设置
函数中定义如下

$this->myServiceMockup = $this->getMockBuilder(MyService::class)
            ->disableOriginalConstructor()
            ->setMethods(['myMethod'])
            ->getMock();
在我的测试函数中,我设置了这样的期望值

$this->myServiceMockup->expects($this->once())
            ->method('myMethod')
            ->with($this->exactly(1), 'myName')
            ->willReturn($this->exactly(1));
这意味着当我只触发myMethod函数一次时,它将返回整数1

所以我正在测试的方法有这行代码

$myIntValue = $this->myService->myMethod($number, $name);
在这一行之后,当我运行测试时,
$myIntValue
应该是1,测试应该继续,这是我对此的理解

但是我得到了这个错误

方法名称的期望失败等于 为调用调用调用了1次参数0

My\Path\To\Class\MyService::myMethod(1,'myName')不匹配 期望值

1与预期的类型“对象”不匹配

没有任何意义,因为
myMethod
需要一个整数和一个字符串

public function myMethod($number, $name)
{
    return $this->table->save($number, $name);
}
有人能解释一下我在这里做错了什么,因为我没有主意。

是一个调用计数匹配器(比如
one()
any()
),用于作为
expects()
方法的参数

只需更换:

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


willReturn()
也接受“原样”的值

您没有正确使用
$this->with

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