我面临以下问题。我写了一个函数,它在给定所需参数的情况下创建一个连接对象 (AMQPConnection)。现在想写相应的单元测试。如果没有运行 RabbitMQ 代理,我只是不知道该怎么做。这是有问题的功能:
public function getConnection($hostKey, array $params)
{
$connection = null;
try {
$connection = new AMQPConnection(
$params['host'],
$params['port'],
$params['username'],
$params['password'],
$params['vhost']
);
// set this server as default for next connection connectionAttempt
$this->setDefaultHostConfig($hostKey, $params);
return $connection;
} catch (\Exception $ex) {
if ($this->isAttemptExceeded()) {
return $connection;
} else {
// increment connection connectionAttempt
$this->setConnectionAttempt($this->getConnectionAttempt() + 1);
return $this->getConnection($hostKey, $params);
}
}
}
最佳答案
您通常不会将这样的代码作为单元测试来测试,因为结果更有可能告诉您服务器已正确安装,而不是您的代码正在运行。
您是否测试 PDO 是否返回有效的数据库连接?
如果您测试您的安装而不是测试 php c 库是否正常工作,这可能是有意义的。
关于PHPUnit RabbitMQ : write test for create connection function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30343887/