如何使用正则表达式在SimpleTest中编写AssertTags测试?
时间:2020-03-06 14:42:36 来源:igfitidea点击:
我希望测试一个将生成lorem ipsum
文本的函数,但是它会在html标签中生成。所以我无法提前知道文本内容,但是我知道html结构。那就是我要测试的。也许文本的长度在一定范围内。
所以我想知道的是,assertTags是否可以用下面的方式做到这一点:
Result = "<p>Some text</p>"; Expected = array( '<p' , 'regex', '/p' ); assertTags(resutl, expected)
我在CakePHP上使用SimpleTest,但我认为这应该是一个普遍的问题。
解决方案
扩展SimpleExpectation类,然后在assert语句中使用新的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());
$expected = array( '<p', 'preg:/[A-Za-z\.\s\,]+/', '/p' );