2010-05-20 52 views
0
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; 

} 

我的断言是: 1)。 $ rfid是一个长度为5个字符的字符串 2)。我得到一个有效的结果集帮我写一个PHPUnit测试以下方法

谢谢

此外,假设我有以下的单元测试代码:

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() { 

} 

公共职能testGetAllVideosByRfid(){

***** *我应该在这里放什么*****

} 
+0

我其实不明白,你打算如何单元测试这个程序的入口?这不应该是你的功能验证吗?像 - (is_string($ rfid)&& strlen($ rfid)== 5) – jpabluz 2010-05-20 15:59:44

+0

我编辑了我的问题以提供更多详细信息。 对不起 – jini 2010-05-20 16:04:42

回答

1

你需要分散你的数据库,ty通过一个数据库抽象层,你会模拟出来。因此,在具有您正在使用的方法的对象上添加 - > setDatabase()等。那么你的设置()内{...}你将数据库对象设置为一个模拟:

$this->object->setDatabase($mockDb); 

然后你会改变

$结果= mysql_query($查询); $ count2 = mysql_num_rows($ result);

使用某种形式的PDO - 这样就可以使用PDO Sql Lite调用setDatabase()。例如:

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. 
    } 
} 

通常这都会在PDO SQLite的实现,这样没有真正的数据库将进行/刚创建的单元测试&,它将生活和测试死了,任何开发人员随时随地能无需配置即可使用它。