文件大小超过40MB的单元测试方法

时间:2017-02-15 12:01:55

标签: c# unit-testing nmock-2.1

我的DTO声明如下

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

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

由于DocumentFile属性声明为byte []数组类型,我无法为DocumentFile属性赋值。

有人可以建议我如何为这种情况编写单元测试方法。

2 个答案:

答案 0 :(得分: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 */
            }
        }
    }
}

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

答案 1 :(得分:1)

这样的东西
[TestMethod]
[ExpectedException(typeof(BlaBlaException), "Exceptiion string")]
public void DocumentFile_set_WhenDocumentFileSetOver40Mb_ShouldThrowExceptionBlaBla {
   DocumentFile = new byte [45000000];
}