我是新來的單元測試在PHP中,我有點麻煩。無論是因爲我使用的是Cake框架,還是因爲我習慣了Java方式,指出我遇到了問題。當單元測試時傳遞數據變量的功能
我正在寫一個模型函數的測試,該函數在提交表單時被調用。該函數接收兩個參數,我認爲我正確通過了,並且沒有收到一個數據對象作爲參數。我的問題是如何填充該「數據」對象?當我運行測試時,我不斷收到「undefined index」錯誤。
我已經嘗試了嘲笑數據和使用燈具,但誠實地說,我沒有得到這個東西。以下是我的模型功能,其次是我的測試代碼。
public function isUniqueIfVerified($check, $unverified){
$found = false;
if ($this->data['Client']['client_type_id'] == 5) {
$found = $this->find ('first', array (
'conditions' => array (
$check,
$this->alias . '.' . $this->primaryKey . ' !=' => $this->id,
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
));
} else {
$found = $this->find ('first', array (
'conditions' => array (
$check,
$this->alias . '.' . $this->primaryKey . ' !=' => $this->id
),
'fields' => array (
'Client.id'
)
));
}
if ($found) {
return false;
} else {
return true;
}
}
這就像我的測試功能的52版本,所以隨時隨地做任何你想做的事情。我在考慮嘲笑數據會更容易和更快,因爲我只需要'model_type_id'來處理我的Model函數中的條件,但是我無法使'data'對象工作,所以我切換到了燈具。 ..沒有成功。
public function testIsUniqueIfVerified01() {
$this->Client = $this->getMock ('Client', array (
'find'
));
$this->Client->set(array(
'client_type_id' => 1,
'identity_no' => 123456789
));
//$this->Client->log($this->Client->data);
$check = array (
'identity_no' => '1234567890123'
);
$unverified = null;
$this->Client = $this->getMockforModel("Client",array('find'));
$this->Client->expects($this->once())
->method("find")
->with('first', array (
'conditions' => array (
"identity_no" => "1234567890123",
"Client.id" => "7711883306236",
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
))
->will($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified));
unset ($this->Client);
}
再次說到Cake,我更加專注於PHP單元測試,所以請隨時解釋我出錯的地方。
謝謝!