[英]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.