![](/img/trans.png)
[英]How to test that only specific methods of a mock were called with PHPUnit
[英]PHPUnit: how to test that methods are called in incorrect order?
我想使用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();
}
有沒有更優雅的方式來做到這一點? 謝謝!
您需要涉及任何InvocationMocker
才能使您的期望有效。 例如,這應該工作:
public function test_at_constraint()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first')->with();
$x->expects($this->at(1))->method('second')->with();
$x->second();
$x->first();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.