【问题标题】:Symfony2: CRUD-generated functional test fails incorrectlySymfony2:CRUD 生成的功能测试错误地失败
【发布时间】:2013-10-26 03:28:56
【问题描述】:

在由 CRUD 生成器生成的简单且稍加修改的功能测试中,爬虫测试错误地失败。这是通过检查断言之前创建的外部文件中的内容来确定的。并通过页面的元素检查。而通过手动运行jquery代码$("td:contains('Test')").length;那么为什么会测试失败呢?

测试类:

class ApplianceControllerTest extends WebTestCase {

    private $client;

    public function __construct() {
        $this->client = static::createClient(array(), array(
         ...
        ));
        $this->client->followRedirects();
    }

    public function testCompleteScenario() {
        // Create a new entry in the database
        $crawler = $this->client->request('GET', '/appliance/');
        $crawler = $this->client->click($crawler->selectLink('Create a new entry')->link());

        // Fill in the form and submit it
        $form = $crawler->selectButton('Create')->form(array(
            'appliance[appliance]' => 'Test',
                // ... other fields to fill
        ));

        $this->client->submit($form);
//        $crawler = $this->client->followRedirect();
        $content = $this->client->getResponse()->getContent();
        file_put_contents('somefile', $content);

        // Check data in the show view
        $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")');
}

输出:

缺少元素 td:contains("Test") 断言 0 更大时失败 大于 0。

捕获的内容摘录:

<div class="title">Appliance</div>
<div class="width40">

    <table class="record_properties">
        <tbody>
            <tr>
                <th>Id</th>
                <td>14</td>
            </tr>
            <tr>
                <th>Appliance</th>
                <td>Test</td>
            </tr>
            <tr>
                <th>Enabled</th>
                <td>Yes</td>
            </tr>
        </tbody>
    </table>

【问题讨论】:

  • 你为什么注释掉$crawler = $this-&gt;client-&gt;followRedirect();?
  • @mattexx:它被注释掉了,因为它抛出了一个错误:“LogicException:请求没有被重定向。”
  • 嗯。你调查过那个错误吗?
  • @mattexx:不,主要是因为我不知道去哪里看或如何处理它。 CRUD 的东西都很稀疏。
  • 提示:表示您的表单提交不成功。

标签: php symfony functional-testing


【解决方案1】:

总结评论线程:

由于错误“LogicException:请求未重定向”,您已注释掉 $crawler = $this-&gt;client-&gt;followRedirect();。此错误表示表单提交不成功。

正如您所发现的,从 __construct() 中删除行 $this-&gt;client-&gt;followRedirects(); 并恢复行 $crawler = $this-&gt;client-&gt;followRedirect(); 可以修复您的测试。

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 2015-06-08
    • 2014-04-30
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多