2012-07-27 3 views
0

이 질문과 관련된 문제가 있습니다. CakePHP "with" method in mock object don't Workmock 객체의 "with"메소드가 null 값을 반환합니다.

그러나 제공된 대답은 나를 위해 일하지 않습니다.

여기에 문제가 있습니다. 컨트롤러 테스트 케이스 코드 :

public function testCreate() { 
$batches = $this->generate('ShippingBatches', array(
    'components' => array(
     'Session', 
     'Auth' => array('user', '_getUser') 
     ) 
)); 

$batches->Auth->expects($this->once())->method('user') 
    ->with('id') 
    ->will($this->returnValue(1)); 

$batches->Session 
    ->expects($this->once()) 
    ->method('setFlash'); 

$this->testAction('/shippingbatches/create', array('data' => '[3,6]', 'method' => 'post')); 
} 

컨트롤러 코드 :

public function create(){ 
//Some code here 
$d['ShippingBatch']['user_id'] = $this->Auth->user('id'); 
$this->ShippingBatch->save($d); 
//some code here 
} 

오류 : SQLSTATE [23000] : 무결성 제약 조건 위반 : 1,048 열 'USER_ID는'널 테스트 케이스가 될 수 없습니다 ShippingBatchesControllerTestCase (testCreate)

답변

0

해결책은 사용자가 정적 함수이므로 expects() 대신 staticExpects()를 사용하는 것입니다.

$batches->Auth->staticExpects($this->once())->method('user') 
     ->with('id') 
     ->will($this->returnValue(1)); 
관련 문제