作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个接口(interface),它有一个方法来创建队列客户端和消息工厂的实例。我已经创建了相同的具体实现,现在我正在尝试对用于将消息发送到队列的功能进行单元测试。我已经为相同的代码编写了以下代码:
private readonly Mock<IQueueClientProvider> queueClientProvider;
private readonly Mock<IJsonSerializer> jsonSerializer;
private readonly Mock<ILogger<QueueConnector>> logger;
private readonly ModelLogger<QueueConnector> modelLogger;
private readonly QueueConnector queueConnector;
private readonly Mock<QueueClient> queueClient;
public QueueConnectorTests()
{
queueClientProvider = new Mock< IQueueClientProvider >();
queueClient = new Mock< QueueClient >();
logger = new Mock< ILogger< QueueConnector >>();
jsonSerializer = new Mock< IJsonSerializer >();
modelLogger = new ModelLogger<QueueConnector>(logger.Object, jsonSerializer.Object);
queueConnector = new QueueConnector(modelLogger, queueClientProvider.Object);
}
[Fact]
public async void ShouldBeAbleToSendMessage()
{
BrokeredMessage brokeredMessage = new BrokeredMessage();
brokeredMessage.CorrelationId = "0HKVUFCD6Q5JL";
queueClientProvider.Setup(q =>q.CreateQueueClient()).Returns(queueClient.Object);
// Act
await queueConnector.SendMessage(brokeredMessage);
// Assert
queueClient.Verify(q => q.SendAsync(brokeredMessage));
}
但出现异常
An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code
Additional information: Parent does not have a default constructor. The default constructor must be explicitly defined.
不知道这里出了什么问题,有人可以帮我解决这个问题吗?
最佳答案
您应该在代码中使用 IQueueClient
接口(interface),而不是 QueueClient 实现。 SendAsync
方法是 ISenderClient
接口(interface)的一部分,IQueueClient
继承自该接口(interface)。
一旦你使用了IQueueClient
,你可以做这样的事情:
var queueClientMock = new Mock<IQueueClient>();
queueClientMock.Setup(x =>
x.SendAsync(It.IsAny<Message>))).Returns(Task.CompletedTask).Verifiable();
关于c# - 如何对调用服务总线队列客户端 SendAsync 方法的类进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40520547/
我是一名优秀的程序员,十分优秀!