我正在尝试对process()
Zend Expressive 应用程序中的中间件方法进行单元测试。为此,我需要模拟出$delegate
方法的参数,该参数是 typeRequestHandlerInterface
并且将具有 method handle()
。
这应该很容易做到,因为我在此测试中使用 Prophesy 成功模拟了其他对象:
每当handle()
调用该方法时,我都会收到以下错误:"Unexpected method call on Double\RequestHandlerInterface\P18:\n - handle(\n Double\ServerRequestInterface\P17:000000004a01de0d000000000617c05e Object (\n 'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)\n )\n )\nexpected calls were:\n - handle(\n\n )"
这是测试。请注意,其他模拟按预期工作,但调用时仍会抛出错误handle()
:$mockDelegate
/**
* @test
*/
public function
testReturnsRedirectResponseForHandlerWhenNoErrorsFoundRequestTypePOST()
{
$renderer = $this->prophesize(TemplateRendererInterface::class);
$renderer
->render('app::contract-form-page', [])
->willReturn('');
$validateSubmitAction = new ValidateSubmitAction(
$this->router->reveal(),
$renderer->reveal(),
get_class($this->container->reveal()),
$this->logger->reveal()
);
$mockRequest = $this->prophesize(ServerRequestInterface::class);
$mockRequest->getMethod()->willReturn('POST');
$mockRequest->getBody()->willReturn(
//create fake object with getContents method
new class {
public function getContents(){ return 'location-number=testLoc&contract-number=1234';}
});
$mockDelegate = $this->prophesize(RequestHandlerInterface::class);
$mockDelegate->handle()->willReturn('');
$response = $validateSubmitAction->process(
$mockRequest->reveal(),
$mockDelegate->reveal()
);
$this->assertInstanceOf(ValidateSubmitAction::class, $validateSubmitAction);
}
这是它试图测试的方法。当该方法应该将请求委托给管道时,似乎会发生错误。看这里:
public function process(ServerRequestInterface $request, RequestHandlerInterface $delegate): ResponseInterface
{
...
// Delegate on to the handler
return $delegate->handle($request); //<-- this is where the error occurs in the unit test
如何RequestHandlerInterface handle()
使用 Prophesy 准确地模拟该方法以实现无错误测试?