【发布时间】:2013-03-09 09:06:38
【问题描述】:
我在一个类中有一个方法可以解析一些 xml。
如果找到标签
我想构建一个单元测试来检查此方法在 status=failure 时是否返回异常。
目前,我无法使用 phpunit 和 MOCKING 完成它?
例子:
<?php
$mock = $this->getMock('Service_Order_Http', array('getResponse'));
$mock->expects($this->any())
->method('getResponse')
->will($this->throwException(new Exception()));
$e = null;
try {
$mock->getResponse();
} catch (Exception $e) {
}
$this->assertTrue($e instanceof Exception, "Method getResponse should have thrown an exception");
//phpunit sends back: PHPUnit_Framework_ExpectationFailedException : Failed asserting that exception of type "Exception" is thrown.
?>
感谢您的帮助
【问题讨论】:
-
重新阅读您的问题后; “Service_Order_Http”是您要测试的类吗?
-
是的,“Service_Order_Http”是我要模拟的类。包含xml解析方法的那个。
-
我已经更新了我的答案,希望这就是你所追求的。
标签: xml exception mocking phpunit