public void Close_IfHasCaptureEventsAndIsClosedIsFalse_ShouldRaiseCaptureEvents()
        {
            const string expectedContent = "Test";
            Encoding expectedEncoding = Encoding.UTF8;
            byte[] buffer = expectedEncoding.GetBytes(expectedContent);

            bool captureRaised = false;
            string actualContent = null;
            Encoding actualEncoding = null;
            object actualSender = null;

            CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), expectedEncoding);
            capturableStream.Capture += delegate(object sender, StreamEventArgs e)
            {
                captureRaised = true;
                actualSender = sender;
                actualContent = e.Content;
                actualEncoding = e.Encoding;
            };

            Assert.IsFalse(captureRaised);

            capturableStream.CapturedStream.Write(buffer, 0, buffer.Length);
            capturableStream.Close();

            Assert.IsTrue(captureRaised);
            Assert.AreEqual(capturableStream, actualSender);
            Assert.AreEqual(expectedContent, actualContent);
            Assert.AreEqual(expectedEncoding, actualEncoding);
        }
 public void Close_ShouldCallCloseOfTheCapturedStream()
 {
     Mock<MemoryStream> memoryStreamMock = new Mock<MemoryStream>();
     memoryStreamMock.Verify(memoryStream => memoryStream.Close(), Times.Never());
     CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>());
     // ReSharper disable PossibleNullReferenceException
     typeof(CapturableStream).GetField("_capturedStream", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(capturableStream, memoryStreamMock.Object);
     // ReSharper restore PossibleNullReferenceException
     capturableStream.Close();
     memoryStreamMock.Verify(memoryStream => memoryStream.Close(), Times.Once());
 }
 public void Close_ShouldSetIsClosedToTrue()
 {
     CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>());
     capturableStream.Close();
     Assert.IsTrue(capturableStream.IsClosed);
 }
 public void Close_IfHasCaptureEventsAndIsClosedIsTrue_ShouldNotRaiseCaptureEvents()
 {
     bool captureRaised = false;
     CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>());
     capturableStream.Capture += (sender, e) => { captureRaised = true; };
     Assert.IsFalse(captureRaised);
     capturableStream.IsClosed = true;
     capturableStream.Close();
     Assert.IsFalse(captureRaised);
 }