gpt4 book ai didi

c# - 如何对调用服务总线队列客户端 SendAsync 方法的类进行单元测试

转载 作者:太空狗 更新时间:2023-10-29 23:12:58 27 4
gpt4 key购买 nike

我有一个接口(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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com