【问题标题】:Symfony functional testing asserting after redirectSymfony 功能测试在重定向后断言
【发布时间】:2023-04-02 03:24:01
【问题描述】:

我目前正在尝试编写功能测试,但在登录并重定向到新页面后我卡住了。 一切正常,直到最后一次断言。重定向工作正常,重定向后的内容页面正常,但我在最后一个断言时收到错误:断言 false 为真失败。我在这里找不到任何可以帮助我解决类似问题的问题。

代码如下:

class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
    $client = static::createClient(array(),
        array(
            'HTTP_HOST' => 'locahost'

        ));



    $crawler = $client->request('GET', '/login');
    echo $client->getRequest()->getUri();


    $form = $crawler->selectButton('Login')->form(array(
        '_username' => 'user',
        '_password' => 'pass',
    ),'POST');


    $client->submit($form);
    $this->assertTrue($client->getResponse()->isRedirect());
    $client->followRedirect();
    echo $client->getRequest()->getUri();

    print_r( $client->getResponse()->getContent());


    $this->assertTrue($crawler->filter('html:contains("New")')->count() > 0);
}

}

【问题讨论】:

  • 我设法通过添加 $crawler = $client->request('GET', '/'); 来部分修复它在最后一个断言之前,但现在的问题是我无法在新页面中找到所有元素,只是其中一些元素,因为页面格式不正确。..
  • 你试过了吗?

标签: symfony redirect functional-testing assertions


【解决方案1】:

你必须得到followredirect()函数返回的爬虫:

$crawler = $client->followRedirect();

【讨论】:

  • 或者干脆$crawler = $client->followRedirect();
  • 已修复!谢谢。
猜你喜欢
  • 2015-10-29
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 2021-02-06
  • 1970-01-01
  • 2019-01-12
相关资源
最近更新 更多