我正在尝试使用重定向来测试 phpunit 的简单注册,我一直有这个错误:
测试类:
<?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/