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)。我得到一个有效的结果集

谢谢你

请假设我有以下单元测试代码:

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

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

}
4

1 回答 1

1

您需要分散您的数据库,通常使用您要模拟的数据库抽象层。因此,在具有您正在使用的方法的对象上添加 ->setDatabase() 等。然后在您的 setUp() { ... } 中,您可以将 Database 对象设置为模拟:

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

然后你会改变

$result = mysql_query($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 中完成,因此不会仅为单元测试创​​建/创建真正的数据库,并且它会随着测试而生死攸关,并且任何地方的任何开发人员都可以在不需要配置的情况下使用它。

于 2010-05-21T19:22:14.147 回答