如何使用预言对Zend Expressive中的RequestHandlerInterface类进行测试双精度?

时间:2018-08-29 18:29:38

标签: unit-testing zend-expressive prophecy

我正在尝试对Zend Expressive应用程序中中间件的process()方法进行单元测试。为此,我需要模拟$delegate类型的方法的RequestHandlerInterface参数,它将具有方法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

如何用Prophesy准确地模拟RequestHandlerInterface handle()方法,以实现无错误的测试?

1 个答案:

答案 0 :(得分:2)

您有这个:$mockDelegate->handle()->willReturn('');,但是应该是这样的:

$handler->handle(Argument::that([$mockRequest, 'reveal']))->willReturn('');

在您的代码中,您希望没有任何参数的情况下调用handle()。但这是通过模拟请求接口的实例调用的。

看看来自zend-expressive-session的示例:

public function testMiddlewareCreatesLazySessionAndPassesItToDelegateAndPersistsSessionInResponse()
{
    $request = $this->prophesize(ServerRequestInterface::class);
    $request
        ->withAttribute(SessionMiddleware::SESSION_ATTRIBUTE, Argument::type(LazySession::class))
        ->will([$request, 'reveal']);

    $response = $this->prophesize(ResponseInterface::class);

    $handler = $this->prophesize(RequestHandlerInterface::class);
    $handler->handle(Argument::that([$request, 'reveal']))->will([$response, 'reveal']);

    $persistence = $this->prophesize(SessionPersistenceInterface::class);
    $persistence
        ->persistSession(
            Argument::that(function ($session) use ($persistence, $request) {
                $this->assertInstanceOf(LazySession::class, $session);
                $this->assertAttributeSame($persistence->reveal(), 'persistence', $session);
                $this->assertAttributeSame($request->reveal(), 'request', $session);
                return $session;
            }),
            Argument::that([$response, 'reveal'])
        )
        ->will([$response, 'reveal']);

    $middleware = new SessionMiddleware($persistence->reveal());
    $this->assertSame($response->reveal(), $middleware->process($request->reveal(), $handler->reveal()));
}