2012-09-26 6 views
1

cake로 만든 응용 프로그램을 테스트하고 있는데 add 메서드를 테스트 할 때 문제가 있음을 발견했습니다. 문제는 DB에 데이터 저장을 테스트 할 때마다 샘플 데이터를 요청과 함께 보내고 난 후에 해당 데이터가 DB에 저장되지만 id 속성 만 설정되어 있음을 알게되었습니다. 다른 필드는 널입니다. 여기 cakephp 및 phpunit을 사용하여 add 메서드를 테스트하십시오.

내가 테스트 할 방법입니다 :

public function admin_add() { 
    if ($this->request->is('post')) { 
     debug($this->request->data); 
     $this->Item->create(); 
     if ($this->Item->save($this->request->data)) { 
      $this->Session->setFlash(__('The item has been saved')); 
      //$this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The item could not be saved. Please, try again.')); 
     } 
    } 
    $products = $this->Item->Product->find('list'); 
    $attributes = $this->Item->Attribute->find('list'); 
    $this->set(compact('products', 'attributes')); 
} 

을 그리고 여기 내 테스트입니다 :

나는이 테스트를 실행하면 제가 DB에 추가 필드를해야한다는
public function testAdmin_add(){ 

$this->Items->request->params['action'] = 'admin_add'; 
$this->Items->request->params['url']['url'] = 'admin/items/add'; 
$data = array(
    'Item' => array(
    'product_id' => 1, 
    'attribute_id' => 9, 
    'title' => 'test_item', 
    'code' => 1, 
    'in_stock' => 1, 
    'batched_stock'=> 1, 
    'allocated' => 0, 
), 
); 

    $this->Collection = $this->getMock('ComponentCollection'); 
    $this->Items->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); 

    $this->Items->Session->expects($this->once()) 
     ->method('setFlash') 
     ->with(__d('items', 'Se ha guardado el item')); 


$this->__setPost($data); 

$this->Items->admin_add(); 

} 

공지 사항 및 테이블의 ID가 자동 증분이기 때문에 해당 항목을 삭제하기 위해 ID를 다시 가져올 수 없습니다.
이제 질문은 : 실제로 데이터를 저장하지 않고 DB 저장을 테스트 할 수있는 방법이 있습니까?

감사합니다.

+0

또 다른 한가지 : add 메서드에서 $ this-> redirect 줄의 주석을 제거하면 테스트가 작동하지 않습니다. –

답변

0

데이터 저장을 방지하려면 조명기를 사용하는 것이 좋습니다. 여기에 문서 사용중인 CakePHP의 어떤 버전을 기반으로 :

테스트 데이터베이스를 사용할 수 있는지 확인하거나 각 테스트 실행 후 해당를 찾을 수 있습니다, 당신의 테이블은 떨어질 것입니다.

관련 문제