我想从PHPSpec开始,所以我正在使用两个简单的类。第一个负责应用百分比减少或放大到数字,第二个负责使用百分比应用程序(如模拟)计算产品价格。
namespace spec\My;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class PercentageToNumberApplyerSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('My\PercentageToNumberApplyer');
}
function it_enlarges_a_number_with_a_given_percentage()
{
$this->enlarge(100, 20)->shouldReturn(120);
$this->enlarge(80, 25)->shouldReturn(100);
$this->enlarge(20, 50)->shouldReturn(30);
}
function it_reduces_a_number_with_a_given_percentage()
{
$this->reduce(100, 20)->shouldReturn(80);
$this->reduce(80, 10)->shouldReturn(72);
$this->reduce(250, 20)->shouldReturn(200);
}
}
<?php
namespace My;
class PercentageToNumberApplyer
{
/**
* Enlarge given number with a given percentage
*
* @param $number
* @param $percentage
* @return float
*/
public function enlarge($number, $percentage)
{
return $this->calculate($number, $percentage) + $number;
}
/**
* Reduce given number with a given percentage
*
* @param $number
* @param $percentage
* @return mixed
*/
public function reduce($number, $percentage)
{
return $number - $this->calculate($number, $percentage);
}
/**
* @param $number
* @param $percentage
* @return float
*/
private function calculate($number, $percentage)
{
return $number * $percentage / 100;
}
}
<?php
namespace spec\My;
use My\PercentageToNumberApplyer;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class PriceCalculatorSpec extends ObjectBehavior
{
function let(PercentageToNumberApplyer $percentageToNumberApplyer)
{
$this->beConstructedWith($percentageToNumberApplyer);
}
function it_calculates_price_discount($percentageToNumberApplyer)
{
$number = 100;
$discount = 20;
$percentageToNumberApplyer->reduce($number, $discount)->shouldBeCalled();
$this->applyDiscountTo($number, $discount)->shouldReturn(80);
}
}
问题是在运行phpspec运行后的上述示例中,结果为:
- it calculates price discount
expected [integer:80], but got null
<?php
namespace My;
class PriceCalculator
{
/**
* @var PercentageToNumberApplyer
*/
private $percentageToNumberApplyer;
/**
* @param PercentageToNumberApplyer $percentageToNumberApplyer
*/
public function __construct(PercentageToNumberApplyer $percentageToNumberApplyer)
{
$this->percentageToNumberApplyer = $percentageToNumberApplyer;
}
/**
* @param $basePrice
* @param $discount
* @return mixed
*/
public function applyDiscountTo($basePrice, $discount)
{
return $this->percentageToNumberApplyer->reduce($basePrice, $discount);
}
}
$priceCalculator = new \My\PriceCalculator(new \My\PercentageToNumberApplyer());
$price = $priceCalculator->applyDiscountTo(100, 20);
$ price有80值...
答案 0 :(得分:1)
您的案件中不需要模拟。 Stub会做的。在PHP Test doubles patterns with prophecy中阅读有关测试双打的更多信息。
而不是嘲笑电话:
$percentageToNumberApplyer->reduce($number, $discount)->shouldBeCalled();
存根:
$percentageToNumberApplyer->reduce($number, $discount)->willReturn(80);
接下来,您只需要预期实际返回的是什么:
$percentageToNumberApplyer->reduce($number, $discount)->willReturn(80);
$this->applyDiscountTo($number, $discount)->shouldReturn(80);
这是因为您不应该关心是否进行了通话。你只对结果感兴趣。
根据经验,您通常会:
大多数情况下,最好将两者分开(命令/查询分离),我们不需要同时模拟和存根。