コード例 #1
0
        public void TestThatWritersAreIsolatedFromEachOther()
        {
            var mockRepository = new MockRepository(MockBehavior.Strict);

            // Mock enabled writer1 that throws on write
            Mock <IAuditLogWriter> mockAuditLogWriter1 = mockRepository.Create <IAuditLogWriter>();

            mockAuditLogWriter1.Setup(writer => writer.Write(It.IsAny <IAuditLogEntryData>())).Throws <NullReferenceException>();

            // Mock enabled writer2
            Mock <IAuditLogWriter> mockAuditLogWriter2 = mockRepository.Create <IAuditLogWriter>();

            mockAuditLogWriter2.Setup(writer => writer.Write(It.IsAny <IAuditLogEntryData>()));

            var auditLog = new AuditLog(new List <IAuditLogWriter> {
                mockAuditLogWriter1.Object, mockAuditLogWriter2.Object
            });

            Assert.DoesNotThrow(() => auditLog.OnLogoff(true, "Admin"));

            // Verify that both writers are called
            mockAuditLogWriter1.Verify(writer => writer.Write(It.IsAny <IAuditLogEntryData>()), Times.Once());
            mockAuditLogWriter2.Verify(writer => writer.Write(It.IsAny <IAuditLogEntryData>()), Times.Once());

            mockRepository.VerifyAll();
        }
コード例 #2
0
        public void TestThatCanContinueAfterFailure()
        {
            var mockRepository = new MockRepository(MockBehavior.Strict);

            // Mock writer that throws on write
            Mock <IAuditLogWriter> mockAuditLogWriter1 = mockRepository.Create <IAuditLogWriter>();

            mockAuditLogWriter1.Setup(writer => writer.Write(It.IsAny <IAuditLogEntryData>())).Throws <NullReferenceException>();

            var auditLog = new AuditLog(new List <IAuditLogWriter> {
                mockAuditLogWriter1.Object
            });

            Assert.DoesNotThrow(() => auditLog.OnLogoff(true, "Admin"));

            mockAuditLogWriter1.Verify(writer => writer.Write(It.IsAny <IAuditLogEntryData>()), Times.Once());

            mockRepository.VerifyAll();
        }