在我的单元测试中需要一些帮助,使用关于等待电子邮件客户端.SendAsync 的最小起订量

问题描述 投票:0回答:1

我真的正在努力为我的电子邮件功能创建单位最小起订量测试,并且想知道是否有人可以帮助我。我想我大部分都是对的,但仍然有一些地方被破坏了。预先感谢

下面是实际的实现代码:

public async Task Email(string htmlContent, string? connectionString, string? subject, string? sender, string? recipient)
        {
            try
            {
                // Encode HTML content to Base-64
                string base64Content = EncodeToBase64(htmlContent);


                Console.WriteLine("Sending email...");


                // Assuming emailClient is an instance of some EmailClient class
                var emailClient = new EmailClient(connectionString); // replace EmailClient with the actual type
                var emailSendOperation = await emailClient.SendAsync(
                    Azure.WaitUntil.Completed,
                    sender,
                    recipient,
                    subject,
                    base64Content);
            }
            catch (Exception ex)
            {
                throw ex;
                //SvrErrorMsg = $"An error occurred: {ex.Message}";
                // Log the exception or take appropriate action
            }
        }

下面是我到目前为止编写的单元测试,但我收到一个编译错误,指出: 'ISetup'不包含'ReturnsAsync'的定义,并且最佳扩展方法重载'SequenceExtensions.ReturnsAsync(ISetupSequentialResult, EmailSendOperation)'需要类型为'Moq.Language.ISetupSequentialResult>'

的接收器

这在以下几行:

EmailClientClientMock.Setup(x => x.SendAsync(WaitUntil.Completed, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null, CancellationToken.None))
                    .ReturnsAsync(emailSendOperationMock.Object);

以下是我迄今为止编写的测试:

        [Test]
        public async Task Email_SendEmailSuccessfully()
        {
            // Arrange
            var configurationMock = new Mock<IConfiguration>();
            configurationMock.Setup(x => x["CommunicationServiceConnectionString"]).Returns("YourConnectionString");
            configurationMock.Setup(x => x["EmailSettings:Subject"]).Returns("YourSubject");
            configurationMock.Setup(x => x["EmailSettings:Sender"]).Returns("YourSender");
            configurationMock.Setup(x => x["EmailSettings:Recipient"]).Returns("YourRecipient");

            var loggerMock = new Mock<ILogger>();

            // Mocking the SecretClient for testing purposes
            var EmailClientClientMock = new Mock<EmailClient>();
            var emailSendOperationMock = new Mock<EmailSendOperation>();

            var _EmailSendOperation = new EmailSendOperation();

            //ERROR:
            //error CS1929: 'ISetup<EmailClient, Task<EmailSendOperation>>' does not contain a definition for 'ReturnsAsync' and the best extension method overload 'SequenceExtensions.ReturnsAsync<EmailSendOperation>(ISetupSequentialResult<Task<EmailSendOperation>>, EmailSendOperation)'
            //requires a receiver of type 'Moq.Language.ISetupSequentialResult<System.Threading.Tasks.Task<NUnit_TestProject.EmailSendOperation>>'
            EmailClientClientMock.Setup(x => x.SendAsync(WaitUntil.Completed, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null, CancellationToken.None))
                .ReturnsAsync(emailSendOperationMock.Object);
            //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

            var myHelper = new Helper();

            await myHelper.SendEmail(configurationMock.Object, "YourHtmlContent", "YourKeyVaultUrl", loggerMock.Object);
c# asp.net-core azure-web-app-service nunit
1个回答
0
投票
EmailClient 类中的 SendAsync 方法返回一个任务。但是,在您的测试设置中,您尝试返回模拟。以下更改应该可以解决此问题。

EmailSendOperation emailSendOperation = new EmailSendOperation(); // Or however you instantiate this // If you need to mock EmailSendOperation // var emailSendOperationMock = new Mock<EmailSendOperation>(); // Use emailSendOperationMock.Object in the Task.FromResult EmailClientClientMock.Setup(x => x.SendAsync( WaitUntil.Completed, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()) ).ReturnsAsync(Task.FromResult(emailSendOperation)); // or emailSendOperationMock.Object if you're mocking
    
© www.soinside.com 2019 - 2024. All rights reserved.