PHPUnit RabbitMQ:创建连接功能的编写测试 [英] PHPUnit RabbitMQ: write test for create connection function
问题描述
我正面临以下问题.我编写了一个函数,该函数根据给定的参数创建连接对象(AMQPConnection).现在我要编写相应的单元测试.我只是不运行RabbitMQ代理就不知道该怎么做.这是有问题的功能:
I'm facing the following problem. I've wrote a function that create a connection object (AMQPConnection) given the required parameters. Now I want to write the corresponding unit test. I just don't know how to do it without having the RabbitMQ broker running. Here is the function in question:
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);
}
}
}
推荐答案
您通常不将这样的代码作为单元测试进行测试,因为结果很可能会告诉您服务器已正确安装,而不是代码可以正常工作
You usually don't test code like this as a Unittest since the result would more likely tell you that your server is installed correctly and not that your code is working.
您是否测试PDO是否返回有效的数据库连接?
Do you test if PDO returns a valid database connection?
如果您测试安装,但没有测试php c库是否正常运行,这可能很有意义.
It could make sense if you test your installation but not to test if the php c libs are working correctly.
这篇关于PHPUnit RabbitMQ:创建连接功能的编写测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!