[英]PHPUnit: how to test that a method hasn't been called "yet", but will be called later in test case?
我需要測試一個方法在測試用例中的某個點沒有被調用,但預計稍后會被調用。 這是一個示例測試:
<?php
class B {
public function doSomething() {}
}
class A {
private $b;
private $buffer = array();
public function __construct($b) {
$this->b = $b;
}
public function x($val) {
$this->buffer[] = $val;
if (count($this->buffer) == 3) {
$this->b->doSomething();
}
}
}
class XTest extends PHPUnit_Framework_TestCase {
public function testB() {
$b = $this->getMock('B');
$a = new A($b);
$a->x(1);
$a->x(2);
// doSomething is not called YET
$b->expects($this->never())->method('doSomething');
// $b->expects($this->at(0))->method('doSomething'); // ??????
$a->x(3);
}
}
在你的情況下,我認為沒有必要這樣做。 另外,我也看不到檢查以前調用的方法。 我寧願檢查參數flush()
是通過以下方式執行的:
$b = $this->getMock('B');
$b->expects($this->exactly(1))
->method('flush')
->with(array(1, 2, 3));
$a = new A($b);
$a->x(1);
$a->x(2);
$a->x(3);
通常,如果調用了其他模擬方法,您需要檢查以前的方法,您可以通過告訴模擬方法確切地何時應該使用at()
調用它們來完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.