如何解决如何编写下面提到的phpunit?
我必须编写单元测试的函数。
public function getlastDebtbalance(float $balance,string $key,float $rateOfInterest) : float {
if ($balance == 0) {
unset($this->debtinterests[$key]);
$lastbalance = $this->getRepayment();
$this-> setLastBalance($lastbalance);
}
elseif ($balance < $this->getNewRepayment()) {
$lastbalance = $this->getRepayment() - $this->calculateRepayment($balance,$this->getNewRepayment(),$rateOfInterest);
$this-> setLastBalance($lastbalance);
}
return $this->getLastBalance();
}
在测试中: 第一个条件如果 balance == 0 那么我需要获取还款并调用 setLastBalance。
$object = $this->getMockBuilder(Test::class)
// NULL = no methods are mocked; otherwise list the methods here.
->setMethods([
"getDebtinterests","getRepayment","getNewRepayment","setLastBalance","calculateRepayment,])
->disableOriginalConstructor()
->getMock();
$object->method('getDebtinterests')
->willReturn(
[
'debt one' => 100,'debt two' => 101,'debt three' => 102,]
);
$object->method('getRepayment')
->willReturn(30.0);
$object->expects($this->any())
->method('setLastBalance')
->with(30.0);
$object->method('getLastBalance')
->willReturn(30.0);
第二个条件,如果 balance
$object = $this->getMockBuilder(Test::class)
// NULL = no methods are mocked; otherwise list the methods here.
->setMethods([
"getDebtinterests",])
->disableOriginalConstructor()
->getMock();
$object->method('getDebtinterests')
->willReturn(
[
'debt one' => 100,]
);
$object->method('getRepayment')
->willReturn(30.0);
$object->method('calculateRepayment')
->with($balance,$rateOfInterest);
$object->expects($this->any())
->method('setLastBalance')
->with(30.0);
$object->method('getLastBalance')
->willReturn(30.0);
我的问题是:
-
这是纠正测试的正确方法吗,我是否需要编写 2 个测试来实现 if else 条件。
-
如何将参数随机化到存根方法中。
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。