【发布时间】:2017-10-26 19:25:56
【问题描述】:
我创建了一个 .net 核心 API,它在 RabbitMQ 队列中推送消息。我使用IOptions 从 .json 文件中读取配置数据并将其添加为依赖项。
下面是我的控制器的代码:
[Route("api/[controller]")]
public class RestController : Controller
{
private RabbitMQConnectionDetail _connectionDetail;
public RestController(IOptions<RabbitMQConnectionDetail> connectionDetail)
{
_connectionDetail = connectionDetail.Value;
}
[HttpPost]
public IActionResult Push([FromBody] OrderItem orderItem)
{
try
{
using (var rabbitMQConnection = new RabbitMQConnection(_connectionDetail.HostName,
_connectionDetail.UserName, _connectionDetail.Password))
{
using (var connection = rabbitMQConnection.CreateConnection())
{
var model = connection.CreateModel();
var helper = new RabbitMQHelper(model, "Topic_Exchange");
helper.PushMessageIntoQueue(orderItem.Serialize(), "Order_Queue");
}
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.BadRequest);
}
return Ok();
}
}
连接详细信息类具有以下属性
public class RabbitMQConnectionDetail
{
public string HostName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
现在我想对它进行单元测试,但由于我要针对黑盒进行测试,所以我无法考虑如何对其进行单元测试并寻求帮助。
连接类
public class RabbitMQConnection : IDisposable
{
private static IConnection _connection;
private readonly string _hostName;
private readonly string _userName;
private readonly string _password;
public RabbitMQConnection(string hostName, string userName, string password)
{
_hostName = hostName;
_userName = userName;
_password = password;
}
public IConnection CreateConnection()
{
var _factory = new ConnectionFactory
{
HostName = _hostName,
UserName = _userName,
Password = _password
};
_connection = _factory.CreateConnection();
var model = _connection.CreateModel();
return _connection;
}
public void Close()
{
_connection.Close();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_connection.Close();
}
}
~ RabbitMQConnection()
{
Dispose(false);
}
}
助手类
public class RabbitMQHelper
{
private static IModel _model;
private static string _exchangeName;
const string RoutingKey = "dummy-key.";
public RabbitMQHelper(IModel model, string exchangeName)
{
_model = model;
_exchangeName = exchangeName;
}
public void SetupQueue(string queueName)
{
_model.ExchangeDeclare(_exchangeName, ExchangeType.Topic);
_model.QueueDeclare(queueName, true, false, false, null);
_model.QueueBind(queueName, _exchangeName, RoutingKey);
}
public void PushMessageIntoQueue(byte[] message, string queue)
{
SetupQueue(queue);
_model.BasicPublish(_exchangeName, RoutingKey, null, message);
}
public byte[] ReadMessageFromQueue(string queueName)
{
SetupQueue(queueName);
byte[] message;
var data = _model.BasicGet(queueName, false);
message = data.Body;
_model.BasicAck(data.DeliveryTag, false);
return message;
}
}
【问题讨论】:
-
您希望在这些测试中编写什么样的断言?
-
由于我正在针对黑盒进行测试,因此我不确定哪个断言会对我有所帮助。 IsEqual 绝对不会有帮助。但是,如果我可以显示消息计数增加或插入时有下一步操作,那可能会有所帮助。
-
您需要测试控制器是否返回正确的
IActionResult,然后检查是否将正确的消息推送到队列中。 -
@ParthoGanguly 抽象,抽象,抽象。您正在尝试测试将其更改为集成测试的实现问题。封装可以模拟的抽象背后的实现问题,以便您可以单独进行单元测试。
-
@ParthoGanguly 你用的是什么版本的 RabbitMQ?
标签: c# unit-testing asp.net-core .net-core rabbitmq