如何构建PHPUnit assertTag()匹配器来匹配这样的HTML表?
我试图验证一个HTML表是否有一个给定的ID,并且有两行,每行包含4个单元格 这是我尝试过的matcher定义,据我所知,应该可以:如何构建PHPUnit assertTag()匹配器来匹配这样的HTML表?,php,html,phpunit,Php,Html,Phpunit,我试图验证一个HTML表是否有一个给定的ID,并且有两行,每行包含4个单元格 这是我尝试过的matcher定义,据我所知,应该可以: $matcher = array( 'tag' => 'table', 'attributes' => array('id' => 'peopleLarge'), 'children' => array( 'count' => 2, 'only' => array(
$matcher = array(
'tag' => 'table',
'attributes' => array('id' => 'peopleLarge'),
'children' => array(
'count' => 2,
'only' => array(
'tag' => 'tr',
'children' => array(
'count' => 4,
'only' => array(
'tag' => 'td'
)
)
)
)
);
$this->assertTag($matcher, $sOutput);
正在测试的输出是:
<table id="peopleLarge">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
然而,这个测试总是失败
我假设我写的matcher定义是错误的,那么比我更了解PHPUnit的人能帮我一把吗
PHPUnit的输出如下:
PHPUnit_Framework_ExpectationFailedException : Failed asserting that <boolean:false> is true.
#0 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\Assert.php(2087): fail()
#1 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\Assert.php(756): assertThat()
#2 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\Assert.php(2048): assertTrue()
#3 C:\_company\t\timeforchildren\webroot\tests\app\views\People_View_LargeTest.php(93): assertTag()
#4 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestCase.php(738): invokeArgs()
#5 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestCase.php(628): runTest()
#6 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestResult.php(666): runBare()
#7 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestCase.php(576): run()
#8 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestSuite.php(757): run()
#9 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestSuite.php(733): runTest()
#10 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\TextUI\TestRunner.php(305): run()
#11 C:\Users\Nils\AppData\Local\Temp\phpunit_webroot_Nils.php(485): doRun()
#12 C:\Users\Nils\AppData\Local\Temp\phpunit_webroot_Nils.php(750): runTest()
#13 C:\Users\Nils\AppData\Local\Temp\phpunit_webroot_Nils.php(853): main()
PHPUnit\u Framework\u expectationfailedeexception:断言为true失败。
#0 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\Assert.php(2087):fail()
#1c:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\Assert.php(756):assertThat()
#2 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\Assert.php(2048):assertTrue()
#3 C:\\u company\t\timeforchildren\webroot\tests\app\views\People\u View\u LargeTest.php(93):assertTag()
#4 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestCase.php(738):invokeArgs()
#5 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestCase.php(628):runTest()
#6 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestResult.php(666):runBare()
#7 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestCase.php(576):run()
#8 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestSuite.php(757):run()
#9 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Framework\TestSuite.php(733):runTest()
#10 C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\TextUI\TestRunner.php(305):run()
#11 C:\Users\Nils\AppData\Local\Temp\phpunit\u webroot\u Nils.php(485):doRun()
#12 C:\Users\Nils\AppData\Local\Temp\phpunit\u webroot\u Nils.php(750):runTest()
#13 C:\Users\Nils\AppData\Local\Temp\phpunit\u webroot\u Nils.php(853):main()
非常感谢,是的,assertTag()的输出很糟糕 我建议将其分解为多个断言和/或测试。我认为您出现错误的原因是它在
'count'=>4
行中变得混乱
我发现,一种很好的方法是逐步建立$matcher
数组,当您遇到错误时,这就是问题所在。相反,你可以写下整件事,然后把它全部注释掉,然后逐渐删除注释
当注释该特定行时,断言成功。当它没有被注释时,我会得到与您相同的错误
奇怪的是:如果输入的值不是4,例如5,则会出现不同的错误:
为foreach()提供的参数无效。
我认为它可能会变得混乱,因为当孩子数本身大于1时,您正在计算孩子的孩子(孙子,如果您愿意的话)。我尝试使用值“8”,因为从技术上讲,您的HTML包含8个td,它们是tr的子项,但这导致了所示的foreach()错误
建议:
'attributes' => array('id' => 'peopleLarge'),
相反,您可以只执行以下操作:
'id' => 'peopleLarge',
(ID本身是assertTag()的条件)是的,assertTag()的输出很糟糕 我建议将其分解为多个断言和/或测试。我认为您出现错误的原因是它在
'count'=>4
行中变得混乱
我发现,一种很好的方法是逐步建立$matcher
数组,当您遇到错误时,这就是问题所在。相反,你可以写下整件事,然后把它全部注释掉,然后逐渐删除注释
当注释该特定行时,断言成功。当它没有被注释时,我会得到与您相同的错误
奇怪的是:如果输入的值不是4,例如5,则会出现不同的错误:
为foreach()提供的参数无效。
我认为它可能会变得混乱,因为当孩子数本身大于1时,您正在计算孩子的孩子(孙子,如果您愿意的话)。我尝试使用值“8”,因为从技术上讲,您的HTML包含8个td,它们是tr的子项,但这导致了所示的foreach()错误
建议:
'attributes' => array('id' => 'peopleLarge'),
相反,您可以只执行以下操作:
'id' => 'peopleLarge',
(ID本身是assertTag()的条件)我通常使用
xpath
添加一些自定义匹配器。这些自定义匹配器可以有更多描述消息
就你而言
private function assertHasXpath($xpath, $message = '', $group = 'Other') {
if (empty($message)) {
$message = "xpath '{$xpath}' not found.";
}
$xpath = $this->xpath($xpath);
$truthiness = count($xpath) > 0;
return $this->assertTrue($truthiness, $message, $group);
}
private function assertHasTableWithColumns($amount_columns, $message = '', $group = 'Other') {
$xpath = "//*//table/tr/td";
$amount_found = count($this->xpath($xpath))
if (empty($message)) {
$message = "No table with {$amount_columns} found. Found {$amount_found}";
}
return $this->assertTrue(($amount_found == $amount_columns), $message, $group);
}
然后,您可以编写非常具有描述性和可读性的测试(也就是说,测试应该是关于什么的)。
我通常使用
xpath
添加一些自定义匹配器。这些自定义匹配器可以有更多描述消息
就你而言
private function assertHasXpath($xpath, $message = '', $group = 'Other') {
if (empty($message)) {
$message = "xpath '{$xpath}' not found.";
}
$xpath = $this->xpath($xpath);
$truthiness = count($xpath) > 0;
return $this->assertTrue($truthiness, $message, $group);
}
private function assertHasTableWithColumns($amount_columns, $message = '', $group = 'Other') {
$xpath = "//*//table/tr/td";
$amount_found = count($this->xpath($xpath))
if (empty($message)) {
$message = "No table with {$amount_columns} found. Found {$amount_found}";
}
return $this->assertTrue(($amount_found == $amount_columns), $message, $group);
}
然后,您可以编写非常具有描述性和可读性的测试(也就是说,测试应该是关于什么的)。
失败信息是什么?查看PHPUnit的输出会有所帮助。不幸的是,它没有那么大的帮助:未能断言这是真的。为我的问题添加了完整的输出。有什么方法可以让我得到更详细的失败信息吗?那太糟糕了。它看起来像是
assertTag()
执行断言,获取布尔结果,并将其传递给assertTrue()
,这会给您留下毫无价值的失败消息。您可以查看assertTag()
的代码,看看您是否可以自己创建一个匹配器,并从对象获取更详细的信息,但我以前没有使用过此断言。失败消息是什么?这将有助于了解PHPU的产量