我有一个使用 Azure 服务总线的类,它基本上通过将消息放入队列来发送电子邮件。当我编写集成测试时,我需要确保我可以接收已发送的消息(不,我不是在测试服务总线)。所以,这是代码:
[Fact]
public async Task PutEmailIntoTheQueue()
{
IlgQueueEmailInfo actual = new IlgQueueEmailInfo("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9afce8f5f7dafbfefee8ffe9e9b4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7d3c8e7c6c3c3d5c2d4d489c4c8ca" rel="noreferrer noopener nofollow">[email protected]</a>", "subject", "test body", MessageBodyType.Text,
"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89d95999194c9b89c9795999196d69b9795" rel="noreferrer noopener nofollow">[email protected]</a>",
"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88d85898184daa88c8785898186c68b8785" rel="noreferrer noopener nofollow">[email protected]</a>");
ServiceBusConnectionStringBuilder sbcb =
new ServiceBusConnectionStringBuilder(SERVICE_BUS_ENDPOINT_S, QUEUE_NAME_S, SAS_KEY_NAME, EMAIL_TESTS_SAS);
QueueClient receiveClient = new QueueClient(sbcb, ReceiveMode.ReceiveAndDelete);
bool hasBeenCalled = false;
//
// Check values here
//
async Task ReceiveMessageHandler(Message message, CancellationToken cancellationToken)
{
Output.WriteLine("Received Message\n");
Assert.True(message.Label != null && message.ContentType != null &&
message.Label.Equals(IlgEmailQueue.MESSAGE_LABEL_S, StringComparison.InvariantCultureIgnoreCase) &&
message.ContentType.Equals("application/json", StringComparison.InvariantCultureIgnoreCase));
byte[] body = message.Body;
string msgJson = Encoding.UTF8.GetString(body);
Output.WriteLine($"json: {msgJson}\n");
IlgQueueEmailInfo emailInfo = JsonConvert.DeserializeObject<IlgQueueEmailInfo>(msgJson);
Assert.NotNull(emailInfo);
Output.WriteLine("emailInfo is not NULL");
Assert.Equal(actual, emailInfo);
Output.WriteLine($"emailInfo equals to actual: {actual == emailInfo}\n");
Output.WriteLine("Setting hasBeenCalled to True");
hasBeenCalled = true;
await receiveClient.CompleteAsync(message.SystemProperties.LockToken);
}
receiveClient.RegisterMessageHandler(
ReceiveMessageHandler,
new MessageHandlerOptions(LogMessageHandlerException) {AutoComplete = true, MaxConcurrentCalls = 1});
await _emailQueue.QueueForSendAsync(actual.FromAddress, actual.ToAddresses[0], actual.Subj, actual.Body, MessageBodyType.Text, actual.CcAddress,
actual.BccAddress);
await Task.Delay(TimeSpan.FromSeconds(5));
Assert.True(hasBeenCalled);
}
但是当我运行它时,我收到一个异常,提示“当前没有事件的测试”。处理此类测试的最佳方法是什么?
最佳答案
您正在以通过线路发送消息并接收消息的方式测试 Azure 服务总线。查看代码,您想要验证消息是否以某种方式收缩。您可以通过测试代码构造的 Message
来缩短它,而不是经历整个发送/接收阶段。或者,创建一个插件来在发送消息之前拦截消息。如果您坚持发送/接收,则在执行断言或超时之前,您的代码不得存在。这是因为消息处理程序是一个同步 API,它将在消息发生更改并由回调处理之前继续执行测试方法。 await Task.Delay(TimeSpan.FromSeconds(5))
不能保证足够。
关于c# - xUnit 测试异步事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54507333/