symfony - PHPUnit LogicException : The request was not redirected. Symfony

标签 symfony phpunit

我正在尝试使用重定向来测试 phpunit 的简单注册,我一直有这个错误:

enter image description here

测试类:

<?php

namespace Tests\AppBundle\Services;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SecuriteTest extends WebTestCase
{

    private $user;

    public function __construct() {
        $this->user = static::createClient();
    }

    public function testsendCreateAccountMail(){

        $crawler = $this->user->request('GET', '/inscription');

        $this->assertEquals(1, $crawler->filter('h1:contains("Informations personnelles")')->count());

        $form = $crawler->selectButton('submitInscription')->form();

        $form['inscription[gender]']     = 'Masculin';
        $form['inscription[name]']       = 'Test';
        $form['inscription[firstName]']      = 'Test';
        $form['inscription[username]']    = 'Test-25@gmail.com';
        $form['inscription[birthDate]']    =  '05/10/1992';
        $form['inscription[pseudo]']    = 'Test';
        $form['inscription[password][first]']    = 'blablabla';
        $form['inscription[password][second]']    = 'blablabla';
        $form['inscription[account]']    = 'Particulier';
        $form['inscription[mentionsLegales]']    = '1';

        $this->user->submit($form);

        $crawler = $this->user->followRedirect();

        $this->assertEquals(1, $crawler->filter('.testFlash:contains("Vous allez recevoir une demande de confirmation sur votre adresse email")')->count());

    }
}

我还尝试使用只有 3 个输入的联系表单进行测试,但我仍然遇到相同的错误。

谢谢

最佳答案

我建议您添加更多关于流程各个方面的测试,例如 http 通信的状态。例如,您可以在每次服务器交互后添加请求正常的内容:

    $crawler = $this->user->request('GET', '/inscription');
    // check server response:
    $this->assertEquals(200, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());
    // or more simply:
    $this->assertTrue($client->getResponse()->isSuccessful());
    // then check the response content
    $this->assertEquals(1, $crawler->filter('h1:contains("Informations personnelles")')->count());

以同样的方式,在遵循重定向测试之前,帖子工作正常并且不会给出像 500 这样的错误:
    $this->user->submit($form);
    $this->assertTrue($client->getResponse()->isRedirection());
    $crawler = $this->user->followRedirect();

希望这有帮助

关于symfony - PHPUnit LogicException : The request was not redirected. Symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44368283/

相关文章:

jenkins - 在 Jenkins 中声明特定的失败测试

docker - PHPUnit 停止我的 Docker 容器

ssl - Symfony2 中的 sfSslRequirementPlugin

configuration - 如何使用symfony2访问 Controller 中的语义配置?

Symfony2 : How to filter the options of an entity-choice form field by a certain attribute?

交响乐团 3 : Call default validator inside a Custom Validator and check it the field is validated

symfony - 保持我的 symfony2 包解耦

php - 通过 ANT 获取和使用父目录

php - 使用 PHPUnit 的 Selenium RC 测试 + 单元测试会引发 Seg 错误

zend-framework - Zend Framework 测试 Controller Zend_Controller_Exception : No default module defined for this application