我正在尝试编写一个测试类,但这是我第一次这样做,所以我从课堂上得到的例子有点迷失。
这是我的Exchange类:
<?php
//require 'User.php';
require 'Product.php';
require 'EmailSender.php';
require 'DatabaseConnection.php';
class Exchange{
private $dateDebut;
private $dateFin;
private $receiver;
private $product;
private $owner;
private $emailSender;
private $dbConnexion;
function __construct($dateDebut, $dateFin, $receiver, $product, $owner)
{
$this->dateDebut = $dateDebut;
$this->dateFin = $dateFin;
$this->receiver = $receiver;
$this->product = $product;
$this->owner = $owner;
$this->emailSender = new EmailSender();
$this->dbConnexion = new DatabaseConnection();
}
public function save($dateDebut, $dateFin)
{
if($this->owner->isValid()){
$checkOwner = true;
}
if($this->receiver->isValid()){
$checkReceiver = true;
}
if($this->product->isValid()){
$checkProduct = true;
}
if(!empty($dateDebut) && $dateDebut > date("Y-m-d")){
$checkDateDebut = true;
}
if(!empty($dateFin) && $dateFin > date("Y-m-d") && $dateFin > $dateDebut){
$checkDateFin = true;
}
if($checkProduct == true && $checkReceiver == true && $checkOwner == true && $checkDateFin == true && $checkDateDebut == true){
if($this->receiver->age < 19){
$this->emailSender->sendMail($this->receiver->getEmail(), "Moins de 18 ans");
}
// enregistrer en base
return 12;
}
else{
return false;
}
}
// Liste des setters
public function setDateDebut($dateDebut)
{
$this->dateDebut = $dateDebut;
}
public function setDateFin($dateFin)
{
$this->dateFin = $dateFin;
}
public function setReceiver($receiver)
{
$this->receiver = $receiver;
}
public function setProduct($product)
{
$this->product = $product;
}
public function setOwner($owner)
{
$this->owner = $owner;
}
public function setEmailSender($emailSender)
{
$this->emailSender = $emailSender;
}
public function setDBConnexion($dbConnexion)
{
$this->dbConnexion = $dbConnexion;
}
// Liste des getters
public function getDateDebut()
{
return $this->dateDebut;
}
public function getDateFin()
{
return $this->dateFin;
}
public function getReceiver()
{
return $this->receiver;
}
public function getProduct()
{
return $this->product;
}
public function getOwner()
{
return $this->owner;
}
public function getEmailSender()
{
return $this->emailSender;
}
public function getDBConnexion()
{
return $this->dbConnexion;
}
}
?>
这是我的&#34;测试&#34;我尝试过的课程。
<?php
要求&#39; Exchange.php&#39;;
class ExchangeTest extends PHPUnit_Framework_TestCase{
private $exchange;
public function testSave()
{
$result = $this->exchange->save("2017-11-18", "2017-11-20");
$this->assertEquals($result, 12);
}
}
任何有关单一测试的帮助都会很好,谢谢,我还得到了函数数据为空的错误。
答案 0 :(得分:0)
看起来你已经定义了一个你没有填充的属性,所以当你去使用它时,没有任何对象可以运行方法并且它出错了。
在大多数单元测试场景中,您希望每个测试尽可能自包含,以避免一个测试导致另一个测试导致级联故障。使这个局部变量应该解决问题:
<div class="tab-pane active"
ng-repeat="tab in tabset.tabs"
ng-class="{active: tabset.active === tab.index}" uib-tab-content-transclude="tab" style>
我想要创建某种public function testSave()
{
$exchange = new Exchange(...);
$result = $this->exchange->save("2017-11-18", "2017-11-20");
$this->assertEquals($result, 12);
}
工厂方法,正确初始化exchange()
实例,以避免在每次测试中重复大量样板。