我正在Symfony2做一些单元测试,我很安静。我有一个非常基础的课程:
<?php
namespace MyBundle\TestBundle\Dto;
class AddressResponse
{
/**
* @var string
*/
public $name;
/**
* @var boolean
*/
public $surname;
/**
* @var array
*/
public $addresses = array();
/**
* @param $name
* @param $surname
* @param array $addresses
*/
public function __construct($name, $surname, array $addresses)
{
$this->name = $name;
$this->surname = $surname;
$this->addresses = $addresses;
}
}
我的测试班:
<?php
namespace MyBundle\TestBundle\Tests\Dto;
use MyBundle\TestBundle\Dto\AddressResponse;
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-05-19 at 14:01:52.
*/
class AddressResponseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var AddressResponse
*/
protected $object;
public $name;
public $surname;
public $address = array();
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = $this->createAddressResponseInstance();
}
public function createAddressResponseInstance()
{
return new AddressResponse(
$this->name,
$this->surname,
$this->address
);
}
public function testAddressResponse()
{
$this->assertTrue($this->object->name, $this->object->surname, $this->object->addresses);
}
}
以上测试是我可以为我的简单calss进行测试,但我收到此错误消息:无法在true上声明null。
除了AssertTrue
之外我还能怎样测试我的简单类?答案 0 :(得分:1)
正如@Crozin已经评论过,你的问题似乎更多与phpunit和使用断言。另外,如果你的类只包含变量和构造函数,那么你没有那么多测试...
如果你真的想测试构造函数是否正确存储值(通常,或者只是为了练习),你可以用以下方式编写测试类(这只是我对如何选择)写它 - 你可以使用类变量和setUp函数来实现同样的目的):
<?php
namespace MyBundle\TestBundle\Tests\Dto;
use MyBundle\TestBundle\Dto\AddressResponse;
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-05-19 at 14:01:52.
*/
class AddressResponseTest extends \PHPUnit_Framework_TestCase
{
public function createAddressResponseInstance($name, $surname, $address)
{
return new AddressResponse($name, $surname, $address);
}
public function testAddressResponse()
{
$name = "John";
$surname = "Doe";
$addresses = null; // for example, you can be more creative here
$object = $this->createAddressResponseInstance($name, $surname, $addresses);
// test the constructor
$this->assertEquals($name, $object->name);
$this->assertEquals($surname, $object->surname);
$this->assertNull($object->addresses);
}
}
检查phpunit manual以获取更多断言选项......