title: Why my GZip unit tests are returning an exception?
body: <p>I have  aproblem with testing my unzip method.</p>
  
  <p>My unit test method is like:</p>
  
  <pre><code>[Fact]
          public void UnzipData_CalledWithByteArrayParameter_ReturnsString()
          {
              _serializationService.CallerName = "";
              byte[] array = new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 0, 11, 117, 205, 49, 15, 130, 48, 16, 134, 255, 255, 114, 43, 148, 220, 157, 165, 45, 55, 234, 204, 98, 216, 140, 3, 9, 23, 37, 209, 18, 67, 53, 38, 134, 255, 46, 168, 139, 3, 243, 247, 189, 121, 14, 47, 232, 159, 32, 148, 195, 110, 136, 73, 99, 170, 135, 78, 47, 32, 47, 136, 237, 85, 65, 160, 214, 211, 185, 141, 144, 195, 163, 31, 251, 52, 126, 174, 93, 155, 150, 137, 145, 130, 65, 103, 152, 26, 44, 165, 172, 4, 109, 65, 155, 210, 57, 79, 25, 178, 32, 206, 213, 114, 221, 235, 237, 174, 99, 210, 238, 175, 129, 105, 202, 191, 56, 175, 226, 219, 97, 13, 174, 12, 178, 97, 219, 80, 16, 14, 98, 185, 32, 31, 188, 117, 62, 67, 90, 133, 127, 205, 12, 31, 223, 154, 207, 196, 62, 247, 0, 0, 0 };
  
              string result = _serializationService.UnzipData(array);
  
              Assert.False(result.Length == 0);
              Assert.True(result.Length &gt; array.Length);
          }
  </code></pre>
  
  <p>but result variable is <code>null</code>. <a href="https://stackoverflow.com/a/7343623">Sut based on this answer</a>:</p>
  
  <pre><code>public string UnzipData(byte[] bytes)
          {
              LoadDictionaries();
              CallerName = _messageService.GetCallerName();
  
              using (var msi = new MemoryStream(bytes))
              using (var mso = new MemoryStream())
              {
                  try
                  {
                      using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                      {
                          CopyTo(gs, mso);
                      }
  
                      return Encoding.UTF8.GetString(mso.ToArray());
                  }
                  catch (Exception e)
                  {
                      _logger.Error(e, MessagesError[CallerName]);
  
                      return null;
                  }
              }
          }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public static void CopyTo(Stream src, Stream dest)
          {
              byte[] bytes = new byte[4096];
  
              int cnt;
  
              while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
              {
                  dest.Write(bytes, 0, cnt);
              }
          }
  </code></pre>
  
  <p>The main problem is that I am not quite sure how to pass <code>byte[]</code> compressed by <code>GZipStream</code> data in my unit test.</p>
  
  <p>As I belive, in test preparation I am not supposed to do something like this? Is not it breaking testability?</p>
  
  <pre><code>[Fact]
          public void UnzipData_CalledWithByteArrayParameter_ReturnsString()
          {
              _serializationService.CallerName = "";
              string someString = "fdfsdfsdfsdfsf";
              byte[] array = _serializationService.ZipData(someString); //using compress method first????
  
              string result = _serializationService.UnzipData(array);
  
              Assert.False(result.Length == 0);
              Assert.True(result.Length &gt; array.Length);
          }
  </code></pre>
  
  <p>Or maybe I can?</p>
  
  <p>ZipData method:</p>
  
  <pre><code>public byte[] ZipData(string data)
          {
              LoadDictionaries();
              CallerName = _messageService.GetCallerName();
              var bytes = Encoding.UTF8.GetBytes(data);
  
              using (var msi = new MemoryStream(bytes))
              using (var mso = new MemoryStream())
              {
                  try
                  {
                      using (var gs = new GZipStream(mso, CompressionMode.Compress))
                      {
                          CopyTo(msi, gs);
                      }
  
                      byte[] toReturn = mso.ToArray();
  
                      return toReturn;
                  }
                  catch (Exception e)
                  {
                      _logger.Error(e, MessagesError[CallerName]);
  
                      return null;
                  }
              }
          }
  </code></pre>
  
  <p>And compressing method returns me:</p>
  
  <p><code>new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 0, 11, 125, 206, 61, 11, 194, 48, 16, 6, 224, 255, 114, 171, 77, 185, 94, 62, 74, 179, 234, 234, 34, 221, 196, 33, 144, 27, 2, 53, 197, 38, 138, 80, 250, 223, 141, 90, 16, 151, 194, 45, 7, 247, 222, 243, 158, 103, 8, 79, 176, 77, 5, 251, 49, 102, 142, 249, 56, 122, 30, 192, 206, 16, 221, 149, 193, 194, 193, 69, 7, 21, 120, 151, 223, 27, 97, 99, 4, 106, 129, 166, 39, 178, 100, 202, 212, 168, 100, 139, 170, 219, 33, 189, 196, 245, 244, 196, 183, 59, 167, 204, 254, 47, 3, 203, 182, 125, 61, 218, 240, 166, 129, 35, 151, 63, 143, 144, 66, 78, 159, 110, 63, 93, 9, 236, 4, 233, 30, 181, 45, 5, 164, 172, 165, 54, 70, 181, 155, 250, 154, 41, 250, 229, 5, 222, 237, 15, 25, 239, 0, 0, 0 }</code></p>
  
  <p>from string:</p>
  
  <p><code>[{\"ix\":1,\"ContentModel\":{\"name\":\"Dana\",\"date\":\"2016-05-06T22:26:26.0437049+02:00\",\"dateRequested\":\"2016-05-06\"}},{\"ix\":2,\"ContentModel\":{\"name\":\"Darlene\",\"visits\":1,\"date\":\"2014-09-25T05:22:33.3566479+02:00\",\"dateRequested\":\"2014-09-25\"}}]</code></p>
  
  <p><strong>UPDATED TEST</strong></p>
  
  <pre><code>[Fact]
          public void UnzipData_CalledWithByteArrayParameter_ReturnsString()
          {
              //string _jsonExample i converted in txtwizard.net/compression
              string _jsonExample = "[{\"ix\":1,\"ContentModel\":{\"name\":\"Dana\",\"date\":\"2016-05-06T22:26:26.0437049+02:00\",\"dateRequested\":\"2016-05-06\"}},{\"ix\":2,\"ContentModel\":{\"name\":\"Darlene\",\"visits\":1,\"date\":\"2014-09-25T05:22:33.3566479+02:00\",\"dateRequested\":\"2014-09-25\"}}]";
              var base64String = "H4sIAAAAAAAA/4WOPwvCMBDFv0tWm3K9/CnNqquLdDMOgdwQqCmaKIL0uxu1irgUbjjevXfvt79bFm6WmaaybD3GTDFvR09DkcopuiOVzbKNi86y4vEuvxWERnNQHHSPaFCXqUGKFmS3AjQAX/uOThdKmfxfzrJpqj79uNR/HijS6+c1pJDTzPzLIzl0HFUPyhQkIWqhtJbtIs+ce/IcHmtZn30RAQAA";
              byte[] array = Convert.FromBase64String(base64String);
  
              _serializationService.CallerName = "";
  
              string result = _serializationService.UnzipData(array);
  
              Assert.Equals(result, _jsonExample); //not equal, added extra backslashes
              Assert.Equal("SomeMethod", _serializationService.CallerName);
              _messageServiceMock.Verify(m =&gt; m.GetCallerName(It.IsAny&lt;string&gt;()), Times.Once);
          }
  </code></pre>