如何使用正则表达式在SimpleTest中编写AssertTags测试?
-
02-07-2019 - |
题
我希望测试一个将生成 lorem ipsum
文本的函数,但它会在html标记中执行。所以我不能事先知道文本内容,但我知道html结构。这就是我想要测试的。也许文本的长度在一定限度内。
所以我想知道的是,断言标签是否可以用下面的方式解释:
Result = "<p>Some text</p>";
Expected = array(
'<p' ,
'regex',
'/p'
);
assertTags(resutl, expected)
我在CakePHP中使用SimpleTest,但我认为这应该是一个普遍的问题。
解决方案
$expected = array(
'<p',
'preg:/[A-Za-z\.\s\,]+/',
'/p'
);
其他提示
扩展SimpleExpectation类,然后在断言语句中使用新的Expectation类
请参阅: http://www.lastcraft.com/expectation_documentation.php#extending一>
给出的示例是验证IP地址但应该适用于您的问题:
class ValidIp extends SimpleExpectation {
function test($ip) {
return (ip2long($ip) != -1);
}
function testMessage($ip) {
return "Address [$ip] should be a valid IP address";
}
}
然后在你的测试中
$this->assert(new ValidIp(),$server->getIp());
不隶属于 StackOverflow