2012-07-27 82 views
0

我正面临着这个问题的确切问题。 CakePHP "with" method in mock object don't WorkCakePHP“with”方法在模拟对象中返回空值

但提供的答案不适用于我。

这是问题所在。 控制器测试用例代码:

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]:完整性约束违规:1048列 'USER_ID' 不能为空 测试用例:ShippingBatchesControllerTestCase (testCreate)

回答

0

解决方法是使用staticExpects()而不是expect(),因为用户是一个静态函数。

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