代码之家  ›  专栏  ›  技术社区  ›  Alexander Morland

如何使用正则表达式在SimpleTest中编写AssertTags测试?

  •  2
  • Alexander Morland  · 技术社区  · 18 年前

    我希望测试一个将生成 lorem ipsum 文本,但它在html标记中这样做。所以我不能预先知道文本内容,但我知道html结构。这就是我想要测试的。也许文本的长度在一定范围内。 所以我想知道的是assertTags是否可以用下面的方式来实现这一点:

    Result = "<p>Some text</p>";
    Expected = array( 
       '<p' ,
       'regex',
       '/p'
    );
    assertTags(resutl, expected)
    

    我将SimpleTest与CakePHP结合使用,但我认为这应该是一个一般性问题。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Alexander Morland    18 年前
    $expected = array(
        '<p',
        'preg:/[A-Za-z\.\s\,]+/',
        '/p'
    );
    
        2
  •  0
  •   Ken    18 年前

    扩展SimpleExpectation类,然后在assert语句中使用新的预期类

    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());