예제 #1
0
        public void Test_SmartLogger_WriteLine_Calls_LogMailer_CreateMessage()
        {
            // In some cases we should combine stubs and mocks.
            // For example, ILogMailer.CreateMessage requires state initialization 
            // required by SmartLogger (stub) and behavior verification (that
            // SmartLogger calls CreateMessage with specified argument).

            // Arrange
            var logMailer = Mock.Of<ILogMailer>(
                lm => lm.CreateMessage(It.IsAny<string>()) == new MailMessage("*****@*****.**", "*****@*****.**"));
            var logWriter = Mock.Of<ILogWriter>();
            var smartLogger = new SmartLogger(logWriter, logMailer);

            // Act
            smartLogger.WriteLine("42");

            // Assert
            var logMailerMock = Mock.Get(logMailer);
            logMailerMock.Verify(lm => lm.CreateMessage("42"));
        }
예제 #2
0
        public void Test_SmartLogger_WriteLine_Calls_LogMailer_CreateMessage()
        {
            // In some cases we should combine stubs and mocks.
            // For example, ILogMailer.CreateMessage requires state initialization
            // required by SmartLogger (stub) and behavior verification (that
            // SmartLogger calls CreateMessage with specified argument).

            // Arrange
            var logMailer = Mock.Of <ILogMailer>(
                lm => lm.CreateMessage(It.IsAny <string>()) == new MailMessage("*****@*****.**", "*****@*****.**"));
            var logWriter   = Mock.Of <ILogWriter>();
            var smartLogger = new SmartLogger(logWriter, logMailer);

            // Act
            smartLogger.WriteLine("42");

            // Assert
            var logMailerMock = Mock.Get(logMailer);

            logMailerMock.Verify(lm => lm.CreateMessage("42"));
        }
        public void Test_WriteLine_Calls_LogWriter_And_LogMailer()
        {
            // Creating different mocks from MockRepository simplifies
            // future verification by calling one verification method
            // on mock repository instead of calling different methods
            // on different mock objects

            // Arrange
            var repo = new MockRepository(MockBehavior.Default);
            var logWriterMock = repo.Create<ILogWriter>();
            logWriterMock.Setup(lw => lw.Write(It.IsAny<string>()));

            var logMailerMock = repo.Create<ILogMailer>();
            logMailerMock.Setup(lm => lm.Send(It.IsAny<MailMessage>()));

            var smartLogger = new SmartLogger(logWriterMock.Object, logMailerMock.Object);

            // Act
            smartLogger.WriteLine("Hello, Logger");

            // Assert
            repo.Verify();

        }