Mar*_*ace 1 bdd phpunit rspec phpspec
在phpspec中,如何测试类属性是否包含特定类型的数组?
例如:
class MyClass
{
private $_mySpecialTypes = array();
// Constructor ommitted which sets the mySpecialTypes value
public function getMySpecialTypes()
{
return $this->_mySpecialTypes;
}
}
Run Code Online (Sandbox Code Playgroud)
我的规格看起来像这样:
public function it_should_have_an_array_of_myspecialtypes()
{
$this->getMySpecialTypes()->shouldBeArray();
}
Run Code Online (Sandbox Code Playgroud)
但我想确保数组中的每个元素都是类型 MySpecialType
什么是在phpspec中做到这一点的最好方法?
您可以使用内联匹配器:
namespace spec;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use MySpecialType;
class MyArraySpec extends ObjectBehavior
{
function it_should_have_an_array_of_myspecialtypes()
{
$this->getMySpecialTypes()->shouldReturnArrayOfSpecialTypes();
}
function getMatchers()
{
return array(
'returnArrayOfSpecialTypes' => function($mySpecialTypes) {
foreach ($mySpecialTypes as $element) {
if (!$element instanceof MySpecialType) {
return false;
}
}
return true;
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1118 次 |
| 最近记录: |