单元测试时将数据变量传递给函数
Pass data variable to function when Unit Testing
我是 PHP 单元测试的新手,遇到了一些小麻烦。无论是因为我使用的是 Cake 框架还是因为我习惯了 Java 方式,请指出我遇到了问题。
我正在为在提交表单时调用的模型函数编写测试。该函数接收两个参数,我认为我正确传递了这两个参数,以及一个未作为参数接收的数据对象。我的问题是如何填充 "data" 对象?当我 运行 测试时,我不断收到 "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 版本,所以你可以随意使用它做任何你想做的事。我在想模拟数据会更容易和更快,因为我真的只需要 'client_type_id' 作为我模型函数中的条件,但我无法让 'data' 对象工作,所以我切换到固定装置...没有成功。
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 单元测试,请随时解释我哪里出错了。
谢谢!
您需要对您的模型函数稍作调整(我将在下面展示),但是您应该能够执行类似这样的操作来传递数据对象中的数据:
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
这不是您使用的 "set",如下所示:
$this->Client->set(array( ...
此外,您模拟了客户端模型,然后 "set" 一些东西,但是就在您进行测试之前,您再次模拟了它。这意味着您将丢弃为在顶部所做的模拟设置的所有内容。您可以执行以下操作来解决您的问题:
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 );
}
这至少应该让您知道该怎么做。希望对您有所帮助!
我是 PHP 单元测试的新手,遇到了一些小麻烦。无论是因为我使用的是 Cake 框架还是因为我习惯了 Java 方式,请指出我遇到了问题。
我正在为在提交表单时调用的模型函数编写测试。该函数接收两个参数,我认为我正确传递了这两个参数,以及一个未作为参数接收的数据对象。我的问题是如何填充 "data" 对象?当我 运行 测试时,我不断收到 "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 版本,所以你可以随意使用它做任何你想做的事。我在想模拟数据会更容易和更快,因为我真的只需要 'client_type_id' 作为我模型函数中的条件,但我无法让 'data' 对象工作,所以我切换到固定装置...没有成功。
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 单元测试,请随时解释我哪里出错了。
谢谢!
您需要对您的模型函数稍作调整(我将在下面展示),但是您应该能够执行类似这样的操作来传递数据对象中的数据:
$this->Client->data = array(
'Client' => array(
'client_type_id' => 5,
'identity_no' => 1234567890123
));
这不是您使用的 "set",如下所示:
$this->Client->set(array( ...
此外,您模拟了客户端模型,然后 "set" 一些东西,但是就在您进行测试之前,您再次模拟了它。这意味着您将丢弃为在顶部所做的模拟设置的所有内容。您可以执行以下操作来解决您的问题:
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 );
}
这至少应该让您知道该怎么做。希望对您有所帮助!