【发布时间】:2013-05-01 08:13:57
【问题描述】:
我想使用 PHPUnit 来测试方法是否以正确的顺序被调用。
我的第一次尝试是在模拟对象上使用->at(),但没有成功。例如,我预计以下操作会失败,但事实并非如此:
public function test_at_constraint()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first');
$x->expects($this->at(1))->method('second');
$x->second();
$x->first();
}
如果事情以错误的顺序调用,我能想到的唯一方法是这样的:
public function test_at_constraint_with_exception()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first');
$x->expects($this->at(1))->method('first')
->will($this->throwException(new Exception("called at wrong index")));
$x->expects($this->at(1))->method('second');
$x->expects($this->at(0))->method('second')
->will($this->throwException(new Exception("called at wrong index")));
$x->second();
$x->first();
}
有没有更优雅的方法来做到这一点?谢谢!
【问题讨论】:
-
我认为这会有所帮助,但我不确定。
-
该页面似乎表明
->at()如果在不同的索引处调用其方法,则不会导致失败,我的第一个测试用例已经证明了这一点。您在该页面上还有什么其他有用的想法吗? -
我在想你可以以某种方式使用
at和verify的组合。