有没有办法为不同的输入参数定义不同的模拟期望?例如,我有一个名为 DB 的数据库层类.此类具有称为Query (string $query)"的方法,该方法在输入时采用 SQL 查询字符串.我可以为这个类(DB)创建模拟并为取决于输入查询字符串的不同查询方法调用设置不同的返回值吗?
Is there any way to define different mock-expects for different input arguments? For example, I have database layer class called DB. This class has method called "Query ( string $query )", that method takes an SQL query string on input. Can I create mock for this class (DB) and set different return values for different Query method calls that depends on input query string?
PHPUnit Mocking 库(默认)仅根据传递给 expects
参数的匹配器和传递的约束来确定期望是否匹配方法
.因此,两个仅在传递给 with
的参数方面不同的 expect
调用将失败,因为两者都会匹配,但只有一个会验证是否具有预期的行为.见实际工作示例后的复现案例.
The PHPUnit Mocking library (by default) determines whether an expectation matches based solely on the matcher passed to expects
parameter and the constraint passed to method
. Because of this, two expect
calls that only differ in the arguments passed to with
will fail because both will match but only one will verify as having the expected behavior. See the reproduction case after the actual working example.
对于您的问题,您需要使用 ->at()
或 ->will($this->returnCallback(
如 关于这个主题的另一个问题
.
For you problem you need to use ->at()
or ->will($this->returnCallback(
as outlined in another question on the subject
.
<?php
class DB {
public function Query($sSql) {
return "";
}
}
class fooTest extends PHPUnit_Framework_TestCase {
public function testMock() {
$mock = $this->getMock('DB', array('Query'));
$mock
->expects($this->exactly(2))
->method('Query')
->with($this->logicalOr(
$this->equalTo('select * from roles'),
$this->equalTo('select * from users')
))
->will($this->returnCallback(array($this, 'myCallback')));
var_dump($mock->Query("select * from users"));
var_dump($mock->Query("select * from roles"));
}
public function myCallback($foo) {
return "Called back: $foo";
}
}
phpunit foo.php
PHPUnit 3.5.13 by Sebastian Bergmann.
string(32) "Called back: select * from users"
string(32) "Called back: select * from roles"
.
Time: 0 seconds, Memory: 4.25Mb
OK (1 test, 1 assertion)
<?php
class DB {
public function Query($sSql) {
return "";
}
}
class fooTest extends PHPUnit_Framework_TestCase {
public function testMock() {
$mock = $this->getMock('DB', array('Query'));
$mock
->expects($this->once())
->method('Query')
->with($this->equalTo('select * from users'))
->will($this->returnValue(array('fred', 'wilma', 'barney')));
$mock
->expects($this->once())
->method('Query')
->with($this->equalTo('select * from roles'))
->will($this->returnValue(array('admin', 'user')));
var_dump($mock->Query("select * from users"));
var_dump($mock->Query("select * from roles"));
}
}
phpunit foo.php
PHPUnit 3.5.13 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 4.25Mb
There was 1 failure:
1) fooTest::testMock
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-select * from roles
+select * from users
/home/.../foo.php:27
FAILURES!
Tests: 1, Assertions: 0, Failures: 1
这篇关于phpunit mock 方法多次调用不同参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!