0

我的 DTO 声明如下

    [MaxLength(maxFileSize, ErrorMessage = "Max Byte Array length is 40MB.")]
    public byte[] DocumentFile { get; set; }

我需要为超过 40MB 的文件大小编写单元测试方法。

由于该DocumentFile属性被声明为 byte[] 数组类型,因此我无法为DocumentFile属性分配任何值。

谁能建议我如何为这种情况编写单元测试方法。

4

2 回答 2

3

编译器和运行时都没有 40MB+1 字节数组的问题:

namespace so42248850
{
    class Program
    {
        class someClass
        {
            /* [attributes...] */
            public byte[] DocumentFile;
        }

        static void Main(string[] args)
        {
            var oversized = new byte[41943041]; /* 40 MB plus the last straw */
            try
            {
                var mock = new someClass
                {
                    DocumentFile = oversized
                };
            } 
            catch(Exception e)
            {
                /* is this the expected exception > test passes/fails */
            }
        }
    }
}

对于生产中的大规模多用户场景,我不会推荐这种方法,因为它可能会造成相当大的内存压力,但对于自动化测试来说应该没问题。

于 2017-02-15T12:12:47.827 回答
1

就像是

[TestMethod]
[ExpectedException(typeof(BlaBlaException), "Exceptiion string")]
public void DocumentFile_set_WhenDocumentFileSetOver40Mb_ShouldThrowExceptionBlaBla {
   DocumentFile = new byte [45000000];
}
于 2017-02-15T12:20:16.950 回答