【发布时间】:2016-09-28 14:05:14
【问题描述】:
我刚刚开始使用 PHPSpec,我真的很喜欢 PHPUnit,尤其是轻松的模拟和存根。无论如何,我试图测试的方法需要一个 Cell 对象数组。如何告诉 PHPSpec 给我一个模拟数组?
我的课的简化版
<?php
namespace Mything;
class Row
{
/** @var Cell[] */
protected $cells;
/**
* @param Cell[] $cells
*/
public function __construct(array $cells)
{
$this->setCells($cells);
}
/**
* @param Cell[] $cells
* @return Row
*/
public function setCells(array $cells)
{
// validate that $cells only contains instances of Cell
$this->cells = $cells;
return $this;
}
}
我的测试的简化版
<?php
namespace spec\MyThing\Row;
use MyThing\Cell;
use PhpSpec\ObjectBehavior;
class RowSpec extends ObjectBehavior
{
function let()
{
// need to get an array of Cell objects
$this->beConstructedWith($cells);
}
function it_is_initializable()
{
$this->shouldHaveType('MyThing\Row');
}
// ...
}
我曾希望我可以执行以下操作,但它随后抱怨它找不到 Cell[]。使用 FQN 它抱怨找不到\MyThing\Cell[]。
/**
* @param Cell[] $cells
*/
function let($cells)
{
// need to get an array of Cell objects
$this->beConstructedWith($cells);
}
我能解决的唯一选择是传递多个类型提示的Cell 参数并手动将它们组合成一个数组。我错过了一些简单的东西吗?
编辑:我正在使用 PHPSpec 2.5.3,不幸的是,服务器目前停留在 PHP 5.3 :-(
【问题讨论】:
标签: php unit-testing phpspec