Testing Symfony 2功能测试
我目前正在尝试用Symfony 2为我的每条路线编写功能测试。现在我正在测试以确保表单的所有元素都存在,并且它们都是正确的输入类型。例如:Testing Symfony 2功能测试,testing,symfony,Testing,Symfony,我目前正在尝试用Symfony 2为我的每条路线编写功能测试。现在我正在测试以确保表单的所有元素都存在,并且它们都是正确的输入类型。例如: // Make sure there is a description field $this->assertTrue($crawler->filter('#form_description')->count() === 1); $this->assertTrue($crawler->filter('#form_descript
// 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>");
//确保有一个描述字段
$this->assertTrue($crawler->filter('#form_description')->count()==1);
$this->assertTrue($crawler->filter(“#表单描述”)->first()->text()=='textarea',“无法验证表单描述是否正确”);
不幸的是,text()似乎什么也不返回,我不知道为什么。通过此测试运行phpunit将提供以下输出:
有1次失败:
1) fixnit\ReportBundle\Tests\Controller\ReportControllerTest::testNew
无法验证#表单描述是否正确
断言false为true失败
如何修复我的测试以获取爬虫过滤器返回的元素的名称?对于爬虫,请说:
爬虫的一个实例表示DomeElement对象的一组(SplObjectStorage),这些对象基本上是可以轻松遍历的节点
因此,根据文档,您应该将代码修改为如下内容:
$formCrawler = $crawler->filter('#form_description')->first();
foreach($formCrawler as $domElement) {
$this->assertTrue(strtolower($domElement->nodeName) == 'textarea', "Unable to verify #form_description is <textarea>");
}
$formCrawler=$crawler->filter('#form_description')->first();
foreach($formCrawler作为$DomeElement){
$this->assertTrue(strtolower($doElement->nodeName)='textarea',“无法验证表单描述是否正确”);
}