phpunit模拟方法使用不同参数进行多次调用 [英] phpunit mock method multiple calls with different arguments
问题描述
是否可以为不同的输入参数定义不同的模拟期望?例如,我有一个称为DB的数据库层类.此类具有称为查询(字符串$ query)"的方法,该方法在输入时采用SQL查询字符串.是否可以为此类(DB)创建模拟并为不同的Query方法调用(取决于输入查询字符串)设置不同的返回值?
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
参数的匹配器和传递给method
的约束来确定期望是否匹配.因此,两个仅传递给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.
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)
再现为什么两个-> with()调用不起作用的原因:
Reproduce why two ->with() calls dont' work:
<?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模拟方法使用不同参数进行多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!