1

在由 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>
4

1 回答 1

2

总结评论线程:

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

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

于 2013-10-25T19:28:56.507 回答