【问题标题】:Symfony 2 functional testingSymfony 2 功能测试
【发布时间】:2012-02-03 22:21:08
【问题描述】:

我目前正在尝试在 Symfony 2 中为我的每条路线编写功能测试。现在我正在测试以确保表单的所有元素都存在并且它们是正确的输入类型。例如:

// Make sure there is a description field
$this->assertTrue($crawler->filter('#form_description')->count() === 1);
$this->assertTrue($crawler->filter('#form_description')->first()->text() == 'textarea', "Unable to verify #form_description is <textarea>");

不幸的是 text() 似乎什么也没返回,我不知道为什么。使用此测试运行 phpunit 会得到以下输出:

有 1 次失败:

1) fixnit\ReportBundle\Tests\Controller\ReportControllerTest::testNew 无法验证 #form_description 是 断言 false 为 true 失败。

如何修复我的测试以获取爬虫过滤器返回的元素的名称?

【问题讨论】:

    标签: testing symfony


    【解决方案1】:

    Docs 为爬虫说:

    Crawler 的一个实例表示一组 DOMElement 对象(SplObjectStorage),它们基本上是您可以轻松遍历的节点

    因此,根据文档,您应该将代码修改为以下内容:

    $formCrawler = $crawler->filter('#form_description')->first();
    foreach($formCrawler as $domElement) {
      $this->assertTrue(strtolower($domElement->nodeName) == 'textarea', "Unable to verify #form_description is <textarea>");
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 2021-02-06
      • 2012-09-10
      • 2022-01-06
      • 1970-01-01
      • 2013-11-12
      • 2012-08-11
      • 2015-10-29
      相关资源
      最近更新 更多