【发布时间】:2012-11-08 20:16:33
【问题描述】:
测试方法:
public function convert(AbstractMessage $message)
{
$data = array();
// Text conversion
$text = $message->getText();
if(null !== $text) {
if(!is_string($text) && (is_object($text)
&& !method_exists($text, '__toString'))) {
throw new UnexpectedTypeException(gettype($text), 'string');
}
$data['text'] = (string) $text;
}
}
如何模拟具有__toString 方法的通用对象(无论类)?
【问题讨论】:
-
你不必嘲笑它。您可以创建自己的真实对象以在测试中使用。我从你的问题中不明白的是你到底想要测试什么。您是否尝试测试对象是否具有 __toString 方法?
-
你是说空班吗?我正在测试@expectedException
-
是的。但又一次。你想测试什么?检查类是否有
__toString方法的方法? -
@Alex 完全正确。如果它是一个没有 __toString 的对象,预计会出现 UnexpectedTypeException。
-
看我的回答。你能从那里开始吗?
标签: php testing mocking phpunit