帮助我为以下方法编写PHPUnit测试
我的主张是: 1). $rfid是一个5个字符长的字符串 2). 我得到了一个有效的结果集 多谢各位 请假设我有以下单元测试代码:帮助我为以下方法编写PHPUnit测试,phpunit,simpletest,Phpunit,Simpletest,我的主张是: 1). $rfid是一个5个字符长的字符串 2). 我得到了一个有效的结果集 多谢各位 请假设我有以下单元测试代码: public function getAvailableVideosByRfid($rfid, $count=200) { $query="SELECT id FROM sometable WHERE rfid='$rfid'"; $result = mysql_query($query); $count2 = mysql_num_rows($result
public function getAvailableVideosByRfid($rfid, $count=200) {
$query="SELECT id FROM sometable WHERE rfid='$rfid'";
$result = mysql_query($query);
$count2 = mysql_num_rows($result);
if ($count2){ //this rfid has been claimed
return 0;
}
公共函数testGetAllVideosByRfid(){
******我应该在这里放什么
class videoSharingTest extends PHPUnit_Framework_TestCase {
/**
* @var videoSharing
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
$this->object = new videoSharing;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
您需要分散您的数据库,通常使用一个数据库抽象层进行模拟。因此,在具有您正在使用的方法的对象上添加->setDatabase()等。然后在setUp()中{…}将数据库对象设置为模拟:
}
然后你会改变
$result=mysql\u query($query);
$count2=mysql\u num\u行($result)
使用某种形式的PDO-以便可以使用PDO Sql Lite调用setDatabase()。例如:
$this->object->setDatabase($mockDb);
通常,这都是在PDO Sqlite中完成的,这样就不会为单元测试创建/创建真正的数据库&它会随着测试而生存和消亡,任何地方的开发人员都可以使用它,而无需进行任何配置。我真的不明白,您如何对该程序的条目进行单元测试?这不应该是一个问题吗函数中的验证?比如-(is_string($rfid)和&strlen($rfid)==5)我编辑了我的问题以提供更多细节。对不起
setUp() { $this->object->setDatabase($mockDb); }
testFunction() {
$rfid = 'the rfid to use in the test';
//make sure no videos exist yet
$this->assertEquals(0, count($this->object->getAvailableVideosByRfid($rfid, ..);
//you may want to assert that it returns a null/false/empty array/etc.
$db = $this->object->getDatabase();
$records = array(... some data ...);
$db->insert($records); //psuedo code
$vids = $this->object->getAvailableVideosByRfid($rfid, ..); //however you map
$this->assertEquals(count($records), count(vids));
foreach($vids as $video) {
//here you would map the $video to the corresponidng $record to make sure all
vital data was stored and retrieved from the method.
}
}