如何在 C# 中使用 xunit 测试变量是否具有字节流

How to test if variable have streams of byte using xunit in C#

我是 XUnit 的新手,我需要为生成数据流(byte[] 数组)并将其设置为变量的方法编写单元测试。我需要验证该变量是否有数据(字节)

方法如下

public class SMTPEmailSender : IEmailSender
{
   private IPDFCreater _pdfCreater;
        private ILogger _log;

        public SMTPEmailSender(IPDFCreater pdfCreater, ILogger log)
        {
            _pdfCreater = pdfCreater;
            _log = log;
        }
public Email BuildEmail(string smptpurl, MailDTO mailRequest)
        {
            Email email = null;
            try
            {
                _
                var otp = new PDFData { OTP = mailRequest.OTP, FaxNumber = mailRequest.FaxNumber, FullName = mailRequest.FullName };
                
                
                email = new Email
                {
                    SMTPServerUrl = smptpurl,
                      .
                      .
                    FromAddressDisplayName = mailRequest.FromAddressDisplayName,
                    ContentStream = new MemoryStream(_pdfCreater.GeneratePDF(otp)),// This line calls a method and return byte array (byte[])
                    AttachmentName = mailRequest.FaxFileName

                };
                
            }
            catch (Exception ex)
            {
                
            }

            return email;
        }
}

我写了单元测试,但我很困惑如何断言变量 ContentStream 是否有数据

[Theory]
    [MemberData(nameof(GetFaxAsMailDTO))]
    public void BuildEmailInfo_ReturnsEmailObject(MailDTO faxMailDTO)
    {
        string SmptpUrl = "testurl";
        var result = _smtpEmailSender.BuildEmailI(SmptpUrl, mailDTO);
        // here I want to check the result.ContentStream have bytes of data or not
    }

检查结果是否包含 ContentStream,然后检查流的长度。

类似于:

var result = _smtpEmailSender.BuildEmailI(SmptpUrl, mailDTO);
Assert.NotNull(result.ContentStream);
Assert.Equal(expectedContentStreamLength, result.ContentStream.Length);