【问题标题】:xUnit test async event handlerxUnit 测试异步事件处理程序
【发布时间】:2019-02-03 20:38:15
【问题描述】:

我有一个使用 Azure 服务总线的类,它基本上通过将消息放入队列来发送电子邮件。当我编写集成测试时,我需要确保我可以接收到已发送的消息(不,我不是在测试服务总线)。所以,这里是代码:

    [Fact]
    public async Task PutEmailIntoTheQueue()
    {
        IlgQueueEmailInfo actual = new IlgQueueEmailInfo("from@address.com", "to@address.com", "subject", "test body", MessageBodyType.Text,
            "email1@domain.com",
            "email2@domain.com");

        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);
    }

但是当我运行它时,我得到一个异常,说“当前没有活动的测试”。处理此类测试的最佳方法是什么?

【问题讨论】:

    标签: c# azure testing azureservicebus xunit


    【解决方案1】:

    您正在测试 Azure 服务总线,您在网络上发送消息并接收它。查看代码,您想验证消息是否以某种方式收缩。您可以通过测试代码构造的Message 将其短路,而不是经历整个发送/接收阶段。或者,创建一个插件以在消息发送之前拦截消息。如果您坚持发送/接收,则在执行断言或超时之前,您的代码不得存在。这是因为消息处理程序是一个同步 API,它将继续执行测试方法之前消息有更改要由回调处理。 await Task.Delay(TimeSpan.FromSeconds(5)) 不保证足够。

    【讨论】:

    • 感谢您的回复。我对任何云的经验是真的应该在集成测试中使用它。否则你会得到惊喜。是的,5 秒可能还不够,但这不是问题所在。我正在考虑类似于 jasmine.js(和角度)的东西,它有 done()
    • 1.您正在测试元数据。 ASB 在发送您的消息时不会更改它。 2. 你只是通过简单地读回来测试身体。同样,ASB 不会改变它。那么通过网络发送的价值是什么? ?无论如何,您可以测试发送/接收。您可以切换到MesaageReceiver.ReceiveAsync() 来简化您的测试方法,而不是使用队列客户端和注册回调。
    • 好吧,也许你是对的,而我正在做的是矫枉过正。 ...但我仍然有疑问:)
    • 如果您的代码发送消息并且您使用MessageReceiver 检索它,有什么疑问?也许我遗漏了一些东西,但您正在执行 ASB 并检索到消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多