我正面临这个问题的确切问题。 模拟对象中的CakePHP“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)