我有3个班级。
class Box{
public $item1;
public $item2;
public function __construct($item1,$item2){
$this->item = $item1;
$this->item2 = $item2;
}
public function getItem1(){
return $this->item1;
}
}
class Details{
public $stuff
public $item1;
public $item2;
public $item3;
public function __construct($stuff){
$this->stuff = $stuff
}
public function setItem1($item){
$this->item1 = $item;
}
public function setItem2($item){
$this->item2 = $item;
}
}
class Crate{
public $box;
private $stuffString = "Stuff";
public function __construct(Box $box){
$this->box = $box;
}
public function getDetails(){
$details = new Details($stuffString);
$details->setItem1($box->item1);
$details->setItem2("Detail");
return $details;
}
}
该方法返回一个 Details 对象,其中包含 Box 对象中的数据。我想为此方法编写测试。Crate->getDetails()
function test_get_details(){
$box = Mockery::mock(Box::class);
$box->shouldReceive('getItem1')->andReturn("BoxItem");
$crate= new Crate($box);
$details = $crate->getDetails();
$this->assertInstanceOf(Details::class,$details);
}
我创建了 Box 类的模拟,并将其传递给 Crate 的构造函数。当我调用它时,它应该返回一个 Details 对象$crate->getDetails();
$item 1 = “BoxItem”
$item 2 = “详细信息”
$item 3 = 空
我知道我可以通过为每个项目做测试这一点等等...但这是最好的方法吗?有没有一些PHPUnit工具来构建所需的Detials结果并进行比较$this->assertEquals("BoxItem",$details->item1);
例如
$this->assertEquals(MockDetailObject,$details)
还是我必须做一系列的断言来确保结果是我所期望的。
注*
我知道对于我的例子来说,这不是什么大不了的事情,我很快就建立了它来解释我的意思。但是在我正在处理的代码中,我遇到了相同类型的问题,除了 Details 对象比 3 个字符串更复杂。
HUX布斯
九州编程