简体   繁体   English

单击链接后重定向测试不会通过

[英]Redirect test won't pass after click on a link

I am writing some dummy test with PHPUnit for Symfony, in order to know a little more about functionals tests in PHP. 我正在用PHPUnit for Symfony编写一些虚拟测试,以便更多地了解PHP中的功能测试。

Here is the problem. 这是问题所在。 This is my test method : 这是我的测试方法:

//Redirecting test by clicking on href
public function testRedirect() {
    $client = static::createClient();
    $crawler = $client->request('GET', '/redirect');

    $link = $crawler->selectLink('Click me !')->link();
    $client->click($link);

    $crawler2 = $client->followRedirect();
    $this->assertTrue(
            $client->getResponse()->isRedirect()
    );
    $this->assertTrue($crawler2->filter('html:contains("Haha, you\'ve been redirect !")')->count() > 0);
}

This is my link : 这是我的链接:

<a href="{{ path('redirect2') }}">Click me !</a>

And the message at destination : 以及目的地信息:

Haha, you've been redirect !

I assume that 我认为

redirect2

is the name of my route. 是我的路线的名称。

Test gives me : 测试给我:

1) AppBundle\Tests\Controller\DefaultControllerTest::testRedirect
LogicException: The request was not redirected.

So my question is : is href considers as a redirection or not ? 所以我的问题是:href是否被视为重定向?

Thanks in advance ! 提前致谢 ! Telest, Telest,

When you follow the redirect, you're already on the "next" page. 当您进行重定向时,您已经在“下一个”页面上。 Just move the row $crawler2 = $client->followRedirect(); 只需移动$crawler2 = $client->followRedirect(); after the the assert 在断言之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM