0

My DTO is declared like below

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

I need to write Unit Test method for File Size more than 40MB.

As the DocumentFile property is declared as byte[] array type, I am unable to assign any value to DocumentFile property.

Can anyone please suggest me how can I write unit test method for this scenario.

Ashok kumar
  • 1,593
  • 4
  • 33
  • 68
  • 4
    The property contains `get; set;`, why are you unable to assign any value to the property? – ColinM Feb 15 '17 at 12:04
  • 1
    is your problem to create a 40MB + 1 byte array? or to assign it to that property? that is not quite clear from your problem description. does it fail to compile, fail at runtime, ... – Cee McSharpface Feb 15 '17 at 12:06
  • Catch the exception when you try to set DocumentFile byte array bigger than 40Mb. Expected exception - max file size exceeded - this is the goal of the test here - you shouldn't be able to set bigger file as it is limited. – h__ Feb 15 '17 at 12:16

2 Answers2

3

Neither compiler nor runtime have a problem with a 40MB+1 byte array:

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 */
            }
        }
    }
}

I would not recommend such an approach for a massively multi-user scenario in production, as it may cause quite some memory pressure, but for an automated test it should be ok.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
1

Something like

[TestMethod]
[ExpectedException(typeof(BlaBlaException), "Exceptiion string")]
public void DocumentFile_set_WhenDocumentFileSetOver40Mb_ShouldThrowExceptionBlaBla {
   DocumentFile = new byte [45000000];
}
h__
  • 761
  • 3
  • 12
  • 41