PHPUnit-为什么预期的方法调用不';不行?
OrderHelper类正在使用OrderRepository类更新订单(通过PHPUnit-为什么预期的方法调用不';不行?,phpunit,magento2,Phpunit,Magento2,OrderHelper类正在使用OrderRepository类更新订单(通过save方法)。我试图测试在OrderHelper::saveOrderSource中是否调用了OrderRepository::save 但它一直在说: 调用1次时,方法名称等于“save”的预期失败。 方法应被调用1次,实际调用0次 请帮忙 setUp()方法: protected function setUp() { $objectManager = new ObjectManager(
save
方法)。我试图测试在OrderHelper::saveOrderSource
中是否调用了OrderRepository::save
但它一直在说:
调用1次时,方法名称等于“save”的预期失败。
方法应被调用1次,实际调用0次
请帮忙
setUp()方法:
protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
->setMethods(['save','getList','get','delete'])
->disableOriginalConstructor()
->getMock();
$this->helper = $objectManager->getObject(
OrderHelper::class,
[
'orderRepository' => $this->orderRepository
]
);
}
public function testSave()
{
$order = $this->createMock(OrderInterface::class);
$this->orderRepository->expects($this->once())
->method('save')
->with($order)
->willReturn($this->helper);
$this->helper->saveOrderSource($order, 1);
}
public function saveOrderSource(OrderInterface $order, $source = null) : OrderHelper
{
if(!is_null($order) && !$source){
$order->setOrderSource($source);
$this->orderRepository->save($order);
}
return $this;
}
测试类:
protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
->setMethods(['save','getList','get','delete'])
->disableOriginalConstructor()
->getMock();
$this->helper = $objectManager->getObject(
OrderHelper::class,
[
'orderRepository' => $this->orderRepository
]
);
}
public function testSave()
{
$order = $this->createMock(OrderInterface::class);
$this->orderRepository->expects($this->once())
->method('save')
->with($order)
->willReturn($this->helper);
$this->helper->saveOrderSource($order, 1);
}
public function saveOrderSource(OrderInterface $order, $source = null) : OrderHelper
{
if(!is_null($order) && !$source){
$order->setOrderSource($source);
$this->orderRepository->save($order);
}
return $this;
}
OrderHelper::saveOrderSource:中的原始方法:
protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
->setMethods(['save','getList','get','delete'])
->disableOriginalConstructor()
->getMock();
$this->helper = $objectManager->getObject(
OrderHelper::class,
[
'orderRepository' => $this->orderRepository
]
);
}
public function testSave()
{
$order = $this->createMock(OrderInterface::class);
$this->orderRepository->expects($this->once())
->method('save')
->with($order)
->willReturn($this->helper);
$this->helper->saveOrderSource($order, 1);
}
public function saveOrderSource(OrderInterface $order, $source = null) : OrderHelper
{
if(!is_null($order) && !$source){
$order->setOrderSource($source);
$this->orderRepository->save($order);
}
return $this;
}
我认为你应该添加这个代码
$this->orderRepository->expects($this->once())
->method('save')
->with($order)
->willReturn($this->helper);
在下一行之前的设置中
$this->helper = $objectManager->getObject(