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($クエリ); $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 に答える