方法预计被调用 1 次,实际调用 0 次 [PHPUnit]
Method was expected to be called 1 times, actually called 0 times [PHPUnit]
我一直在测试、添加和删除行以查看问题出在哪里。我设法删减了太多行,基本上我的测试看起来像那样(当然要看看哪里出了问题,我知道这不是正确的测试):
public function test()
{
$invoice = new Invoice();
$invoiceRepository = $this
->getMockBuilder('\Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock();
$invoiceRepository->expects($this->once())
->method('findOneByNextNote')
->will($this->returnValue($invoice));
$invoiceRepository->findOneByNextNote();
}
还是不行!我从标题中得到一个错误:
Expectation failed for method name is equal to
< string:findOneByNextNote > when invoked 1 time(s). Method was expected
to be called 1 times, actually called 0 times.
我错过了什么?
您可以指定要模拟的方法(而不是完整的 class)以便 class 的行为,因此仅 muck 您想测试的方法如下:
public function testFindOne()
{
$invoice = new Invoice();
$invoiceRepository = $this
->getMockBuilder('\Doctrine\ORM\EntityRepository')
->setMethods(array('findOneByNextNote'))
->disableOriginalConstructor()
->getMock();
$invoiceRepository->expects($this->once())
->method('findOneByNextNote')
->will($this->returnValue($invoice));
$invoiceRepository->findOneByNextNote();
}
所以断言按预期工作
希望对您有所帮助
您的断言失败,因为 EntityRepository 实际上没有名为 findOneByNextNote
的已实现方法,因此它也从未被调用。
然而,神奇的 __call
方法已实现(调用 findOneByNextNote
将由 PHP 引擎的方法重载委托给该方法),因此您可以断言此方法被调用来修正你的断言。
一个可能更好的解决方案是使用该特定方法实际编写一个具体的 InvoiceRepository class(我什至建议一个接口),然后模拟此 class 或接口。
有时在"expects()"指令之前调用方法时会出现这种误解。所以代码看起来不错,但没有用。
例子不好:
$mock = $this
->getMockBuilder(YourClass::class)
->setMethods(['emit'])
->getMock();
$mock->emit();
$mock->expects($this->once())->method('emit');
//Method was expected to be called 1 times, actually called 0 times.
例子好:
$mock = $this
->getMockBuilder(YourClass::class)
->setMethods(['emit'])
->getMock();
$mock->expects($this->once())->method('emit');
$mock->emit();
我一直在测试、添加和删除行以查看问题出在哪里。我设法删减了太多行,基本上我的测试看起来像那样(当然要看看哪里出了问题,我知道这不是正确的测试):
public function test()
{
$invoice = new Invoice();
$invoiceRepository = $this
->getMockBuilder('\Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock();
$invoiceRepository->expects($this->once())
->method('findOneByNextNote')
->will($this->returnValue($invoice));
$invoiceRepository->findOneByNextNote();
}
还是不行!我从标题中得到一个错误:
Expectation failed for method name is equal to < string:findOneByNextNote > when invoked 1 time(s). Method was expected to be called 1 times, actually called 0 times.
我错过了什么?
您可以指定要模拟的方法(而不是完整的 class)以便 class 的行为,因此仅 muck 您想测试的方法如下:
public function testFindOne()
{
$invoice = new Invoice();
$invoiceRepository = $this
->getMockBuilder('\Doctrine\ORM\EntityRepository')
->setMethods(array('findOneByNextNote'))
->disableOriginalConstructor()
->getMock();
$invoiceRepository->expects($this->once())
->method('findOneByNextNote')
->will($this->returnValue($invoice));
$invoiceRepository->findOneByNextNote();
}
所以断言按预期工作
希望对您有所帮助
您的断言失败,因为 EntityRepository 实际上没有名为 findOneByNextNote
的已实现方法,因此它也从未被调用。
然而,神奇的 __call
方法已实现(调用 findOneByNextNote
将由 PHP 引擎的方法重载委托给该方法),因此您可以断言此方法被调用来修正你的断言。
一个可能更好的解决方案是使用该特定方法实际编写一个具体的 InvoiceRepository class(我什至建议一个接口),然后模拟此 class 或接口。
有时在"expects()"指令之前调用方法时会出现这种误解。所以代码看起来不错,但没有用。
例子不好:
$mock = $this
->getMockBuilder(YourClass::class)
->setMethods(['emit'])
->getMock();
$mock->emit();
$mock->expects($this->once())->method('emit');
//Method was expected to be called 1 times, actually called 0 times.
例子好:
$mock = $this
->getMockBuilder(YourClass::class)
->setMethods(['emit'])
->getMock();
$mock->expects($this->once())->method('emit');
$mock->emit();