public void TestSyslogStreamWriterFailingWrite() { var memoryStream = new MemoryStream(); // Setup the mocks var mockRepo = new MockRepository(MockBehavior.Strict); Mock <IStreamProvider> streamProviderMock = mockRepo.Create <IStreamProvider>(); streamProviderMock.Setup(s => s.GetStream()).Returns(memoryStream); streamProviderMock.Setup(s => s.CloseStream()); // Create the message var syslogMessage = new SyslogMessage { Facility = SyslogFacility.ClockDaemon1, Severity = SyslogSeverity.Alert }; Mock <ISyslogMessageSerializer> syslogSerializerMock = mockRepo.Create <ISyslogMessageSerializer>(); syslogSerializerMock.Setup(sm => sm.Serialize(syslogMessage, It.IsAny <Stream>())).Throws <InvalidOperationException>(); // Write a message and see that it fails var streamWriter = new SyslogStreamWriter(streamProviderMock.Object, syslogSerializerMock.Object); Assert.Throws <InvalidOperationException>(() => streamWriter.Write(syslogMessage)); Assert.AreEqual(0, memoryStream.Length); mockRepo.VerifyAll(); }
public void TestSyslogStreamWriterValidWrite() { var memoryStream = new MemoryStream(); // Setup the mocks var mockRepo = new MockRepository(MockBehavior.Strict); Mock <IStreamProvider> streamProviderMock = mockRepo.Create <IStreamProvider>(); streamProviderMock.Setup(s => s.GetStream()).Returns(memoryStream); streamProviderMock.Setup(s => s.Dispose()); string syslogMsg = "Msg" + Guid.NewGuid(); var syslogMessage = new SyslogMessage { Facility = SyslogFacility.ClockDaemon1, Severity = SyslogSeverity.Alert }; Mock <ISyslogMessageSerializer> syslogSerializerMock = mockRepo.Create <ISyslogMessageSerializer>(); syslogSerializerMock.Setup(sm => sm.Serialize(syslogMessage, It.IsAny <Stream>())).Callback <SyslogMessage, Stream>((m, s) => WriteTestDataToStream(s, syslogMsg)); using (var streamWriter = new SyslogStreamWriter(streamProviderMock.Object, syslogSerializerMock.Object)) { streamWriter.Write(syslogMessage); } // Validation memoryStream.Position = 0; var reader = new StreamReader(memoryStream); Assert.AreEqual(syslogMsg, reader.ReadToEnd()); mockRepo.VerifyAll(); }
public void TestSyslogStreamWriterNullMessage() { var mockRepo = new MockRepository(MockBehavior.Loose); Mock <IStreamProvider> streamProviderMock = mockRepo.Create <IStreamProvider>(); Mock <ISyslogMessageSerializer> syslogSerializerMock = mockRepo.Create <ISyslogMessageSerializer>(); var streamWriter = new SyslogStreamWriter(streamProviderMock.Object, syslogSerializerMock.Object); Assert.Throws <ArgumentNullException>(() => streamWriter.Write(null)); }