【问题标题】:behat 3 mink testing redirectionbehat 3 貂皮测试重定向
【发布时间】:2016-04-27 12:59:34
【问题描述】:

在 symfony 3 控制器文件中我有函数:

    /**
     * @Route("/user/registration", name="post_registration")
     * @Method("POST")
     * @return mixed
     */
    public function postRegistration()
    {
        $post = $this->getAllPost();

        $this->curl = $this->get('ApiClient');

        $responseArray = $this->curl->post(
            $this->container->getParameter('api_url') . '/users',
            $post
        );

        if (isset($responseArray['api_key'])) {
            return $this->redirectResponseWithCookie($responseArray['api_key']);
        } else {

            return $this->render(
                static::REGISTRATION_TEMPLATE,
                ['errors' => $responseArray['errors']]
            );
        }
    }

一方面调用函数

redirectResponseWithCookie()

应该重定向页面。

我想测试重定向标头 - 它是否具有正确的位置值。

我在 UserRegistrationContext 类中有函数

class UserRegistrationContext extends BaseContext implements Context, SnippetAcceptingContext 
{

/**
 * @When I register successfully into the system
 * @return null
 */
public function iRegisterSuccessfullyIntoTheSystem()
{

    $this->canIntercept();
    $this->session->getDriver()->getClient()->followRedirects(false);

    // Enters his data
    // Posts to www.notification.guru .
    $this->session->getDriver()->getClient()
        ->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray());

    echo 'test';

}
}

BaseContext 类只有一些辅助函数,它的构造器初始化会话:

$driver = new GoutteDriver();
$this->session = new Session($driver);

这部分可能有误:

$this->session->getDriver()->getClient()->followRedirects(false);

我刚刚从 behat 2.5 获取代码并尝试调整以使其与 behat 3 一起使用,但不确定它是否正确,但至少不会引发错误。

它应该停止重定向,所以我可以得到一个带有

的响应头
getResponseHeaders()

但问题是它尝试重定向,并且代码失败,因为真正的站点还没有在它重定向的地方启动。而且我也无法测试我猜在真正重定向后的标题。

所以我猜重定向必须停止。

如何做到这一点?我找不到信息。

测试失败

$this->session->getDriver()->getClient()
        ->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray());

【问题讨论】:

  • 其实我看到那个代码没有错,运行测试后在控制台看不太清楚。

标签: symfony behat mink


【解决方案1】:

所以我的问题中的代码没有错,正如我在评论中所说的那样 - 我在控制台中看不太清楚。经常发生这样的情况,当我在 SO 上发帖时,我什至没有写完问题,只是从把细节写给其他人我就看到了答案。

$this->session->getDriver()->getClient()->followRedirects(false);

效果很好。

所以要完成如何检查 - 制作类似这样的函数并调用它:

/**
 * @param Behat\Mink\Session $session - mink session
 * @throws \Exception
 * @return null
 */
public function isLoggedIntoApi($session)
{
    $headers = $session->getResponseHeaders();

    if ($headers['Location'][0] != $this->appUrl) {
        throw new \Exception(
            'User is not redirected to ' . $this->appUrl
        );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多