public void TestWithMockLogger()
        {
            var loggerMock = new Mock <ILogger <CalculationService> >();
            var svc        = new CalculationService(loggerMock.Object);

            var result = svc.AddTwoPositiveNumbers(1, 2);

            Assert.AreEqual(3, result);
            loggerMock.Verify(
                m => m.Log(
                    LogLevel.Information,
                    It.IsAny <EventId>(),
                    It.Is <It.IsAnyType>((v, _) => v.ToString().Contains("Adding 1 and 2")),
                    null,
                    It.IsAny <Func <It.IsAnyType, Exception, string> >()),
                Times.Once);

            result = svc.AddTwoPositiveNumbers(-1, 1);
            Assert.AreEqual(0, result);
            loggerMock.Verify(
                m => m.Log(
                    LogLevel.Error,
                    It.IsAny <EventId>(),
                    It.Is <It.IsAnyType>((v, _) => v.ToString().Contains("Arguments should be both positive.")),
                    null,
                    It.IsAny <Func <It.IsAnyType, Exception, string> >()),
                Times.Once);
        }
Beispiel #2
0
        public void TestWithMockLogger()
        {
            var loggerMock = new Mock <ILogger <CalculationService> >();
            var svc        = new CalculationService(loggerMock.Object);
            var result     = svc.AddTwoPositiveNumbers(1, 2);

            Assert.AreEqual(3, result);
            loggerMock.VerifyLog(LogLevel.Information, "Adding 1 and 2");

            result = svc.AddTwoPositiveNumbers(-1, 1);
            Assert.AreEqual(0, result);
            loggerMock.VerifyLog(LogLevel.Error, "Arguments should be both positive.", Times.Once());
        }
Beispiel #3
0
        public void TestWithNullLogger()
        {
            var svc    = new CalculationService(new NullLogger <CalculationService>());
            var result = svc.AddTwoPositiveNumbers(1, 2);

            Assert.AreEqual(3, result);
        }
Beispiel #4
0
        public void Test2()
        {
            var serviceProvider = new ServiceCollection()
                    .AddLogging()
                    .BuildServiceProvider();
            var svcLogger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<CalculationService>();
            var svc = new CalculationService(svcLogger);
            var result = svc.AddTwoPositiveNumbers(2,3);
            Assert.AreEqual(5, result);

           
        }
Beispiel #5
0
        public void TestWithDependencyInjectionLogger()
        {
            var services = new ServiceCollection()
                           .AddLogging(config => config.AddConsole()) // can add any logger(s)
                           .BuildServiceProvider();

            using var loggerFactory = services.GetRequiredService <ILoggerFactory>();
            var svc    = new CalculationService(loggerFactory.CreateLogger <CalculationService>());
            var result = svc.AddTwoPositiveNumbers(1, 2);

            Assert.AreEqual(3, result);
        }
Beispiel #6
0
        public void TestWithConsoleLogger()
        {
            using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
            var logger = loggerFactory.CreateLogger <CalculationService>();
            var svc    = new CalculationService(logger);
            var result = svc.AddTwoPositiveNumbers(1, 2);

            Assert.AreEqual(3, result);

            // If you are using .NET Core 2.1.
            //using (var loggerFactory = new LoggerFactory().AddConsole())   // Need to use "using" in order to flush Console output
            //{
            //    var logger = loggerFactory.CreateLogger<CalculationService>();
            //    var svc = new CalculationService(logger);
            //    var result = svc.AddTwoPositiveNumbers(1, 2);
            //    Assert.AreEqual(3, result);
            //}
        }
        public void TestWithConsoleLogger()
        {
            using (var loggerFactory = new LoggerFactory().AddConsole())   // Need to use "using" in order to flush Console output
            {
                var logger = loggerFactory.CreateLogger <CalculationService>();
                var svc    = new CalculationService(logger);
                var result = svc.AddTwoPositiveNumbers(1, 2);
                Assert.AreEqual(3, result);
            }
            // The loggerFactory above is obsolete as of .NET Core 2.2.
            // Please use the following method in .NET Core 3.0.

            //using (var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()))
            //{
            //    var logger = loggerFactory.CreateLogger<CalculationService>();
            //    var svc = new CalculationService(logger);
            //    var result = svc.AddTwoPositiveNumbers(1, 2);
            //    Assert.AreEqual(3, result);
            //}
        }
Beispiel #8
0
 public void Test1()
 {
     var svc = new CalculationService(new NullLogger<CalculationService>());
     var result = svc.AddTwoPositiveNumbers(3, 5);
     Assert.AreEqual(8, result, "Result is not correct");
 }