【发布时间】:2016-11-29 19:38:26
【问题描述】:
我是 PHP 单元测试的新手,遇到了一些麻烦。无论是因为我使用的是 Cake 框架还是因为我习惯了 Java 方式,都指出我遇到了问题。
我正在为在提交表单时调用的模型函数编写测试。该函数接收两个我认为我正确传递的参数,以及一个未作为参数接收的数据对象。我的问题是如何填充该“数据”对象?运行测试时,我不断收到“未定义索引”错误。
我已经尝试过模拟数据和使用固定装置,但老实说,我不明白这些东西。下面是我的模型函数,后面是我的测试代码。
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 版本,所以你可以随意使用它。我在想模拟数据会更容易和更快,因为我的模型函数中的条件只需要'client_type_id',但我无法让那个'数据'对象工作,所以我切换到了固定装置。 ..没有成功。
public function testIsUniqueIfVerified01() {
$this->Client = $this->getMock ( 'Client', array (
'find'
) );
$this->Client->set(array(
'client_type_id' => 1,
'identity_no' => 1234567890123
));
//$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 单元测试,所以请随时解释我哪里出错了。
谢谢!
【问题讨论】:
标签: unit-testing cakephp phpunit