2010-05-20 2 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

+0

자세한 내용을 제공하기 위해 제 질문을 편집했습니다. 죄송합니다. – jini

답변

1

데이터베이스를 분산해야합니다. 당신이 조롱하는 데이터베이스 추상화 레이어를 사용하면됩니다. 따라서 사용중인 메서드가있는 객체에 -> setDatabase() 등을 추가합니다. 그런 다음 설정() 안에 {...} 당신은 모의에 데이터베이스 개체를 설정합니다 :

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

당신이

$ 결과 =는 mysql_query ($ 쿼리)를 변경할 것입니다; $ count2 = mysql_num_rows ($ result);

PDO의 일부 형식을 사용하여 PDO Sql Lite로 setDatabase()를 호출 할 수 있습니다. 예를 들어 일반적으로이 모두가 PDO SQLite는에서 수행 될 수

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

진정한 데이터베이스가 이루어지지 될 수 있도록/그냥 살고 테스트와 다이 및 개발자가 어디 수 것이라는 단위 테스트 & 생성 구성을 필요로하지 않고 사용하십시오.