Unit testing 单元测试时将数据变量传递给函数
我对PHP中的单元测试还不熟悉,现在有点麻烦。无论是因为我使用的是Cake框架,还是因为我习惯了Java方式,都要指出我有问题 我正在为提交表单时调用的模型函数编写测试。该函数接收两个参数,我认为我正确地传递了这两个参数,以及一个未作为参数接收的数据对象。我的问题是如何填充“数据”对象?当我运行测试时,我不断得到“未定义索引”错误 我尝试过模仿数据和使用装置,但老实说,我不懂这些东西。下面是我的模型函数,后面是我的测试代码Unit testing 单元测试时将数据变量传递给函数,unit-testing,cakephp,phpunit,Unit Testing,Cakephp,Phpunit,我对PHP中的单元测试还不熟悉,现在有点麻烦。无论是因为我使用的是Cake框架,还是因为我习惯了Java方式,都要指出我有问题 我正在为提交表单时调用的模型函数编写测试。该函数接收两个参数,我认为我正确地传递了这两个参数,以及一个未作为参数接收的数据对象。我的问题是如何填充“数据”对象?当我运行测试时,我不断得到“未定义索引”错误 我尝试过模仿数据和使用装置,但老实说,我不懂这些东西。下面是我的模型函数,后面是我的测试代码 public function isUniqueIfVerified($
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;
}
}
公共函数为唯一验证($check,$unverified){
$found=false;
如果($this->data['Client']['Client\u type\u id']==5){
$found=$this->find('first',数组(
“条件”=>数组(
美元支票,
$this->alias.'..$this->primaryKey.!='=>$this->id,
“客户端类型\u id”=>5
),
“字段”=>数组(
“Client.id”
)
) );
}否则{
$found=$this->find('first',数组(
'条件'=>数组(
美元支票,
$this->alias.'..$this->primaryKey.!='=>$this->id
),
“字段”=>数组(
“Client.id”
)
) );
}
如有($已找到){
返回false;
}否则{
返回true;
}
}
这就像我的测试函数的52版本,所以可以随意使用它。我想模拟数据会更容易更快,因为我只需要模型函数中条件的“client\u type\u id”,但我无法让“data”对象工作,所以我切换到fixture。。。没有成功
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 );
}
公共功能测试IQueifVerified01(){
$this->Client=$this->getMock('Client',数组(
“找到”
) );
$this->Client->set(数组(
'客户端类型\u id'=>1,
“标识号”=>1234567890123
));
//$this->Client->log($this->Client->data);
$check=数组(
“标识号”=>“1234567890123”
);
$unverified=null;
$this->Client=$this->getMockforModel(“客户端”,数组('find'));
$this->Client->expected($this->once())
->方法(“查找”)
->带('first',数组(
“条件”=>数组(
“标识号”=>“1234567890123”,
“Client.id”=>“7711883306236”,
“客户端类型\u id”=>5
),
“字段”=>数组(
“Client.id”
)
))
->威尔($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check,$unverified));
未设置($this->Client);
}
再说一次,我对Cake非常生疏,更具体地说是PHP单元测试,所以请随意解释我的错误所在
谢谢 您需要对模型函数进行轻微调整(我将在下面介绍),但是您应该能够执行类似的操作来传递数据对象中的数据:
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
这不是您使用的“设置”,如下所示:
$this->Client->set(array( ...
另外,您模拟了客户机模型,然后“设置”了一些东西,但是在您进行测试之前,您再次模拟了它。这意味着你要扔掉你在顶部做的模拟的所有东西。您可以采取以下措施解决您的问题:
public function testIsUniqueIfVerified01() {
$this->Client = $this->getMock ( 'Client', array (
'find'
) );
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
$check = array (
'identity_no' => '1234567890123'
);
$unverified = null;
$this->Client->expects($this->once())
->method("find")
->with($this->identicalTo('first'), $this->identicalTo(array(
'conditions' => array (
$check,
"Client.id !=" => 1,
'client_type_id <>' => 5
),
'fields' => array (
'Client.id'
)
)))
->will($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified));
unset ( $this->Client );
}
公共功能测试IQueifVerified01(){
$this->Client=$this->getMock('Client',数组(
“找到”
) );
$this->Client->data=array(
“客户端”=>数组(
“客户端类型id”=>5,
“标识号”=>1234567890123
));
$check=数组(
“标识号”=>“1234567890123”
);
$unverified=null;
$this->Client->expected($this->once())
->方法(“查找”)
->使用($this->identicalTo('first'),$this->identicalTo(数组(
“条件”=>数组(
美元支票,
“Client.id!=”=>1,
“客户端类型\u id”=>5
),
“字段”=>数组(
“Client.id”
)
)))
->威尔($this->returnValue(false));
$this->assertTrue($this->Client->isUniqueIfVerified($check,$unverified));
未设置($this->Client);
}
这至少应该让你知道该怎么做。希望有帮助