1

我正在尝试对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 准确地模拟该方法以实现无错误测试?

4

1 回答 1

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()));
}
于 2018-08-31T06:26:49.803 回答