期望使用PHPUnit模拟对象的部分数组
发布时间:2020-12-13 13:14:48 所属栏目:PHP教程 来源:网络整理
导读:使用()子句在 PHPUnit模拟中测试多个数组键的最佳方式是什么? 例如,要测试方法调用第二个参数是否包含一个’foo’键的数组: $this-stubDispatcher-expects($this-once()) -method('send') -with('className',$this-arrayHasKey('foo')); 我想做的是像$this
|
使用()子句在
PHPUnit模拟中测试多个数组键的最佳方式是什么?
例如,要测试方法调用第二个参数是否包含一个’foo’键的数组: $this->stubDispatcher->expects($this->once())
->method('send')
->with('className',$this->arrayHasKey('foo'));
我想做的是像$this-> arrayHasKey(‘foo’,’bar’),而实际上并不匹配数组的确切内容.
您可以直接将断言传递给 – > with(),但它们的名称不同.
有关列表,请查看源:https://github.com/sebastianbergmann/phpunit/blob/3.5/PHPUnit/Framework/Assert.php#L2097及以下.一切都不以“assert”开头,并返回一个新的PHPUnit_Framework_ *. 虽然我认为@ David的答案是有用的,我认为这种方法,使用logicalAnd()是一个更干净/更容易阅读. $mock->expects($this->once())->method("myMethod")->with(
$this->logicalAnd(
$this->arrayHasKey("foo"),$this->arrayHasKey("bar")
)
);
工作实例 <?php
class MyTest extends PHPUnit_Framework_TestCase {
public function testWorks() {
$mock = $this->getMock("stdClass",array("myMethod"));
$mock->expects($this->once())->method("myMethod")->with(
$this->logicalAnd(
$this->arrayHasKey("foo"),$this->arrayHasKey("bar")
)
);
$array = array("foo" => 1,"bar" => 2);
$mock->myMethod($array);
}
public function testFails() {
$mock = $this->getMock("stdClass",$this->arrayHasKey("bar")
)
);
$array = array("foo" => 1);
$mock->myMethod($array);
}
}
产量 phpunit assertArrayKey.php PHPUnit 3.5.13 by Sebastian Bergmann. .F Time: 0 seconds,Memory: 6.50Mb There was 1 failure: 1) MyTest::testFails Expectation failed for method name is equal to <string:myMethod> when invoked 1 time(s) Parameter 0 for invocation stdClass::myMethod(array( <string:foo> => <integer:1> )) does not match expected value. Failed asserting that an array has the key <string:bar>. /home/edo/test/assertArrayKey.php:27 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
