【发布时间】:2013-12-05 01:58:00
【问题描述】:
还在学习如何测试php
我现在有一个工作界面(我认为) - 其中一个功能旨在创建一系列我现在想要测试的记录。我承认我对测试知之甚少,因此问题多于知识。
所以
我的界面目前是这样的:
interface TicketCreatorInterface {
public function createTicket($input, $book);
}
我的“存储库”类如下所示:
Class TicketCreator implements TicketCreatorInterface {
protected $ticket;
public function __construct(TicketAudit $ticketAudit)
{
$this->ticket = $ticketAudit;
}
public function createTicket($input, $book) {
$counter = $input['start'];
while($counter <= $input['end']) {
$this->$ticket->create(array(
'ticketnumber'=>$counter,
'status'=>'unused',
'active'=>1
));
$this->ticket->book()->associate($book);
$counter = $counter+1;
}
return $counter;
}
我的测试尝试如下:
public function testCreateCreatesTickets(TicketCreatorInterface $ticketCreator) {
//arrange
$book = Mockery::mock('Book');
//act
$response = $ticketCreator->createTicket(array('start'=>1000, 'end'=>1001), $book);
// Assert...
$this->assertEquals(true, $response);
}
我首先尝试了没有输入界面,因为没有这个我得到了没有对象的错误。我尝试在界面上创建一个实例,但你不能这样做,所以在函数中使用类型提示
我运行测试时得到的错误是:
Argument 1 passed to TicketCreatorTest::testCreateCreatesTickets() must implement interface TicketCreatorInterface, none given
创建界面对我来说是一种新方法,所以还不完全了解它。
那么我怎样才能测试这个函数是否按预期创建票证?
我已经在内存数据库中使用 sqlite 测试过模型
【问题讨论】:
标签: php unit-testing interface phpunit