Ejemplo n.º 1
0
        public void Analyze_WebServiceThrows_SendsEmail()
        {
            MockRepository mocks       = new MockRepository();
            IWebService    stubService = mocks.Stub <IWebService>();

            IEmailService mockEmail = mocks.StrictMock <IEmailService>();

            using (mocks.Record())
            {
                stubService.LogError("aaa");
                LastCall.Constraints(Is.Anything());
                LastCall.Throw(new Exception("가짜 예외"));

                mockEmail.SendEmail("a", "subject", "가짜 예외");
            }

            LogAnalyzer2 log = new LogAnalyzer2();

            log.Service = stubService;
            log.Email   = mockEmail;

            string tooShortFileName = "abc.ext";

            log.Analyze(tooShortFileName);

            mocks.VerifyAll();
        }
Ejemplo n.º 2
0
        public void Analyze_EmptyFile_ThrowException()
        {
            LogAnalyzer2 la = new LogAnalyzer2();

            la.Analyze("myemptyfile.txt");
            // Rest of test
        }
Ejemplo n.º 3
0
        public void Analyze_WebServiceThrows_SendsEmail()
        {
            FakeWebService stubService = new FakeWebService();

            stubService.ToThrow = new Exception("fake exception");

            FakeEmailService mockEmailService = new FakeEmailService();

            LogAnalyzer2 log = new LogAnalyzer2(stubService, mockEmailService);

            string tooShortFileName = "abc.ext";

            log.Analyze(tooShortFileName);

            EmailInfo expectedEmail = new EmailInfo()
            {
                Body    = "fake exception",
                To      = "*****@*****.**",
                Subject = "can't log"
            };

            StringAssert.AreEqualIgnoringCase(expectedEmail.Body, mockEmailService.Email.Body);
            StringAssert.AreEqualIgnoringCase(expectedEmail.To, mockEmailService.Email.To);
            StringAssert.AreEqualIgnoringCase(expectedEmail.Subject, mockEmailService.Email.Subject);

            Assert.AreEqual(expectedEmail.Body, mockEmailService.Email.Body);
            Assert.AreEqual(expectedEmail.To, mockEmailService.Email.To);
            Assert.AreEqual(expectedEmail.Subject, mockEmailService.Email.Subject);

            Assert.AreEqual(expectedEmail, mockEmailService.Email);//失败,不知为何
        }
Ejemplo n.º 4
0
        public void Analyze_EmptyFile_ThrowsException()
        {
            FakeTheLogger();

            LogAnalyzer2 la = new LogAnalyzer2();

            la.Analyze("myemptyfile.txt");
            // rest of test
        }
Ejemplo n.º 5
0
        public void Analyze_LoggerThrows_CallsWebService()
        {
            var mockWebService = Substitute.For <IWebService>();
            var stubLogger     = Substitute.For <ILoger>();

            stubLogger.When(logger => logger.LogError(Arg.Any <string>())).
            Do(info => { throw new Exception("fake exception"); });
            var analyzer2 = new LogAnalyzer2(stubLogger, mockWebService);

            analyzer2.MinNameLength = 10;
            analyzer2.Analyze("Short.txt");
            mockWebService.Received().Write(Arg.Is <string>(s => s.Contains("fake exception")));
        }
Ejemplo n.º 6
0
        public void Analyze_LoggerThrows_CallsWebService()
        {
            var mockWebService = Substitute.For <IWebService>();
            var stubLogger     = Substitute.For <ILogger>();

            stubLogger.When(logger => logger.LogError(Arg.Any <string>())).Do(info => { throw new Exception("sztuczny wyjątek"); });

            var analyzer = new LogAnalyzer2(stubLogger, mockWebService);

            analyzer.MinNameLenght = 10;
            analyzer.Analyze("short.txt");

            mockWebService.Received().Write(Arg.Is <ErrorInfo>(s => s.Message.Contains("sztuczny wyjątek")));
        }
        public void Analyze_WebServiceThrows_SendsEmail()
        {
            FakeWebService stubService = new FakeWebService();

            stubService.ToThrow = new Exception("fake exception");
            FakeEmailService mockEmail        = new FakeEmailService();
            LogAnalyzer2     log              = new LogAnalyzer2(stubService, mockEmail);
            string           tooShortFileName = "abc.txt";

            log.Analyze(tooShortFileName);
            StringAssert.Contains("*****@*****.**", mockEmail.To);
            StringAssert.Contains("fake exception", mockEmail.Body);
            StringAssert.Contains("can't log", mockEmail.Subject);
        }
Ejemplo n.º 8
0
        public void Analyze_TooShortFileName_CallsWebService()
        {
            // arrange
            var mockWs   = new MockWebService();
            var analyzer = new LogAnalyzer2 {
                Email = null, Service = mockWs
            };
            const string tooShortFileName = "abc.ext";

            // act
            analyzer.Analyze(tooShortFileName);

            // assert
            Assert.AreEqual("Filename too short:abc.ext", mockWs.LastError);
        }
Ejemplo n.º 9
0
        public void Analyze_LoggerThrows_CallsWebServiceWithNSub()
        {
            var mockWebService = MockRepository.GenerateMock <IWebService>();
            var stubLogger     = MockRepository.GenerateStub <ILogger>();

            stubLogger.Stub(x => x.LogError("Too short name: a.txt"))
            .IgnoreArguments()
            .Throw(new Exception("fake exception"));

            var analyzer = new LogAnalyzer2(stubLogger, mockWebService);

            analyzer.MinNameLength = 10;
            analyzer.Analyze("Short.txt");

            mockWebService.AssertWasCalled(x => x.Write(Arg.Text.Contains("fake exception")));
        }
Ejemplo n.º 10
0
        public void Analyze_LoggerThrows_CallsWebService()
        {
            FakeWebService mockWebService = new FakeWebService();
            FakeLogger2    stubLogger     = new FakeLogger2();

            stubLogger.WillThrow = new Exception("fake exception");
            LogAnalyzer2 analyzer2 = new LogAnalyzer2(stubLogger, mockWebService);

            analyzer2.MinNameLength = 8;
            string tooShortFileName = "abc.ext";

            analyzer2.Analyze(tooShortFileName);

            Assert.That(mockWebService.MessageToWebService,
                        Does.Contain("fake exception"));
        }
Ejemplo n.º 11
0
        public void Analyze_Logger2Throws_CallsWebService_NSub()
        {
            var mockWebService = NSubstitute.Substitute.For <IWebService2>();
            var stubLogger     = NSubstitute.Substitute.For <ILogger2>();

            //simulation of exception on any input
            stubLogger
            .When(logger => logger.LogError(Arg.Any <String>()))
            .Do(info => { throw new Exception("fake Exception"); });

            var analyzer = new LogAnalyzer2(stubLogger, mockWebService);

            analyzer.MinNameLength = 10;
            analyzer.Analyze("Short.txt");

            mockWebService.Received().Write(Arg.Is <String>(s => s.Contains("fake Exception")));
        }
Ejemplo n.º 12
0
        public void Analyze_Logger2Throws_CallsWebService()
        {
            FakeWebService2 mockWebService = new FakeWebService2();
            FakeLogger2     stubLogger     = new FakeLogger2();

            stubLogger.WillThrow = new Exception("fake Exception");

            var analyzer2 = new LogAnalyzer2(stubLogger, mockWebService);

            analyzer2.MinNameLength = 8;

            string tooShortfileName = "abc.txt";

            analyzer2.Analyze(tooShortfileName);

            Assert.That(mockWebService.MessageToWebService, Is.SamePathOrUnder("Error from Logger2: fake Exception"));
        }
Ejemplo n.º 13
0
        //[Test]
        public void Analyze_WebServiceThrows_SendsEmail()
        {
            var stubService = new StubService();

            stubService.ToThrow = new Exception("fake exception");

            var mockEmail = new MockEmailService();

            var log = new LogAnalyzer2(stubService, mockEmail);

            string tooShortFileName = "abc.ext";

            log.Analyze(tooShortFileName);

            Assert.AreEqual("a", mockEmail.To);
            Assert.AreEqual("fake exception", mockEmail.Body);
            Assert.AreEqual("subject", mockEmail.Subject);
        }
Ejemplo n.º 14
0
        public void Analyze_TooShortFileName_CallsWebService()
        {
            FakeWebService stubService = new FakeWebService();

            stubService.ToThrow = new Exception("fake exception");
            FakeEmailService mockEmail = new FakeEmailService();

            LogAnalyzer2 log = new LogAnalyzer2(stubService, mockEmail);
            string       tooShortFileName = "abc.ext";

            log.Analyze(tooShortFileName);
            EmailInfo expectedEmail = new EmailInfo
            {
                Body    = "fake exception",
                To      = "*****@*****.**",
                Subject = "can’t log"
            };

            Assert.AreSame(expectedEmail, mockEmail.email);
        }
Ejemplo n.º 15
0
        public void Analyze_LoggerThrows_CallsWebService()
        {
            //Arrange
            var mockWebService = Substitute.For <IWebService>();
            var stubLogger     = Substitute.For <ILogger>();

            // Simulate exception on any input
            stubLogger.When(
                logger => logger.LogError(Arg.Any <String>()))
            .Do(info => { throw new Exception("fake exception"); });

            var analyzer = new LogAnalyzer2(stubLogger, mockWebService);

            // Comment
            analyzer.MinNameLength = 10;
            //Act
            analyzer.Analyze("123.txt");

            //Assert
            mockWebService.Received().Write(Arg.Is <String>(s => s.Contains("fake exception")));
        }
Ejemplo n.º 16
0
        //[Test]
        public void Analyze_WebServiceThrows_SendsEmail2()
        {
            var mocks       = new MockRepository();
            var stubService = mocks.Stub <IWebService>();
            var mockEmail   = mocks.StrictMock <IEmailService>();

            using (mocks.Record())
            {
                stubService.LogError("whatever");
                LastCall.Constraints(new Anything());
                LastCall.Throw(new Exception("fake exception"));

                mockEmail.SendEmail("a", "subject", "fake exception");
            }

            LogAnalyzer2 log = new LogAnalyzer2(stubService, mockEmail);
            string       tooShortFileName = "abc.ext";

            log.Analyze(tooShortFileName);
            mocks.VerifyAll();
        }
Ejemplo n.º 17
0
        public void Analyze_TooShortFileName_WebServiceThrowsAndSendsEmail()
        {
            // arrange
            var stubService = new StubService {
                ToThrow = new Exception("fake exception")
            };
            var mockEmail = new MockEmailService();
            var log       = new LogAnalyzer2
            {
                Service = stubService,
                Email   = mockEmail
            };
            const string tooShortFileName = "abc.ext";

            // act
            log.Analyze(tooShortFileName);

            // assert
            Assert.AreEqual("a", mockEmail.To);
            Assert.AreEqual("fake exception", mockEmail.Body);
            Assert.AreEqual("subject", mockEmail.Subject);
        }