在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:包含("测试")失败断言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>
答案 0 :(得分:2)
总结评论主题:
由于错误“LogicException:请求未被重定向”,您已注释掉$crawler = $this->client->followRedirect();
。此错误表示表单提交不成功。
正如您所发现的那样,从__construct()中删除行$this->client->followRedirects();
并恢复行$crawler = $this->client->followRedirect();
会修复您的测试。