【发布时间】:2021-06-11 11:32:13
【问题描述】:
我有一个名为ip_requestTest.php 的测试类,它具有以下功能:
public function testSetTimestampCount()
{
$validData = [
'id' => '99',
'ip' => '127.0.0.1',
'timestamp' => '2021-06-03 10:28:43.000'
];
$table = 'ip';
$stmt = $this->createMock(\PDOStatement::class);
$stmt->expects($this->once())
->method('execute')
->with($validData)
->willReturn(true);
global $pdo;
$pdo = $this->createMock('PDO');
$pdo->expects($this->once())
->method('prepare')
->with("INSERT INTO {$table} id,address,timestamp VALUES (:ip, :id, :timestamp)")
->willReturn($stmt);
$ipStmt = new ip_request();
$ipStmt->setTimestampCount($validData);
}
现在最后一行是问题,$ipStmt->setTimestampCount($validData);
我实际的 setTimestampCount() 函数不只接受一个参数。
看起来像这样:
function setTimestampCount($ip,$id,$conn)
{
$query = $conn->prepare ("INSERT INTO `ip` (`id`, `address` ,`timestamp`)VALUES (:id,:ip,CURRENT_TIMESTAMP)");
$query ->bindValue(':id', $id, PDO::PARAM_INT);
$query ->bindValue(':ip', $ip, PDO::PARAM_STR);
$query->execute();
}
任何人都可以帮助我,或者给我任何关于如何正确测试此功能的线索吗?
如何将数据包含到测试函数调用中?
谢谢
更新 1:
尝试了 splat 运算符的建议,还删除了时间戳插入,因为这可以通过插入数据自动完成。
仍然无法正常工作,我得到的错误是
Too few arguments to function 2 passed, exactly 3 expected.
我显然知道这意味着什么,但是 splat 操作员如何处理这个问题?如果有的话?
【问题讨论】:
-
你想在这里测试什么?
-
setTimestampCount() 函数谢谢,但就像我解释的那样,它需要 3 个参数,其中一个是实际连接,还有 id 和 ip,但我似乎无法弄清楚如何在测试期间传递这些变量,而不是数组 $validData,这是我希望能够作为测试的一部分传递的。
-
使用 splat 运算符
...它是$ipStmt->setTimestampCount(...$validData); -
试过了,并给出了更新,当我使用该行时仍然无法正常工作。