我有一个接口,它有一个方法来创建队列客户端和消息工厂的实例。我已经创建了相同的具体实现,现在我正试图对用于发送消息队列的功能进行单元测试。我写了下面的代码:
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));
}
但是得到异常
类型为'System '的异常。日志含义发生NotSupportedExceptionMscorlib.dll,但未在用户代码中处理
附加信息:父类没有默认构造函数。必须显式定义默认构造函数。
有人能帮我这个不知道出了什么问题吗?
您应该在代码中使用IQueueClient
接口而不是QueueClient实现。SendAsync
方法是IQueueClient
继承的ISenderClient
接口的一部分。
一旦你使用了IQueueClient
,你可以这样做:
var queueClientMock = new Mock<IQueueClient>();
queueClientMock.Setup(x =>
x.SendAsync(It.IsAny<Message>))).Returns(Task.CompletedTask).Verifiable();