mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-16 14:39:28 +00:00
Replace Moq with NSubstitute (#51)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using NSubstitute;
|
||||
using System.Logging;
|
||||
|
||||
namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
||||
@@ -8,13 +8,13 @@ namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
||||
[TestClass]
|
||||
public class CVLoggerProviderTests
|
||||
{
|
||||
private readonly Mock<ILogsWriter> logsWriterMock = new();
|
||||
private readonly ILogsWriter logsWriterMock = Substitute.For<ILogsWriter>();
|
||||
private CVLoggerProvider cVLoggerProvider;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
this.cVLoggerProvider = new CVLoggerProvider(this.logsWriterMock.Object);
|
||||
this.cVLoggerProvider = new CVLoggerProvider(this.logsWriterMock);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -30,13 +30,6 @@ public class CVLoggerProviderTests
|
||||
{
|
||||
this.cVLoggerProvider.LogEntry(new Log());
|
||||
|
||||
this.logsWriterMock.Verify();
|
||||
}
|
||||
|
||||
private void SetupLogsWriter()
|
||||
{
|
||||
this.logsWriterMock
|
||||
.Setup(u => u.WriteLog(It.IsAny<Log>()))
|
||||
.Verifiable();
|
||||
this.logsWriterMock.ReceivedWithAnyArgs().WriteLog(Arg.Any<Log>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Logging;
|
||||
|
||||
@@ -10,13 +10,13 @@ namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
||||
[TestClass]
|
||||
public class CVLoggerTests
|
||||
{
|
||||
private readonly Mock<ICVLoggerProvider> cvLoggerProviderMock = new();
|
||||
private readonly ICVLoggerProvider cvLoggerProviderMock = Substitute.For<ICVLoggerProvider>();
|
||||
private CVLogger cVLogger;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
this.cVLogger = new CVLogger(string.Empty, this.cvLoggerProviderMock.Object);
|
||||
this.cVLogger = new CVLogger(string.Empty, this.cvLoggerProviderMock);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -59,17 +59,8 @@ public class CVLoggerTests
|
||||
[TestMethod]
|
||||
public void Log_CallsLogsProvider()
|
||||
{
|
||||
this.SetupLoggerProvider();
|
||||
|
||||
this.cVLogger.Log(LogLevel.Debug, new EventId(), "Some message", new Exception(), new Func<string, Exception, string>((s, e) => string.Empty));
|
||||
|
||||
this.cvLoggerProviderMock.Verify();
|
||||
}
|
||||
|
||||
private void SetupLoggerProvider()
|
||||
{
|
||||
this.cvLoggerProviderMock
|
||||
.Setup(u => u.LogEntry(It.IsAny<Log>()))
|
||||
.Verifiable();
|
||||
this.cvLoggerProviderMock.ReceivedWithAnyArgs().LogEntry(Arg.Any<Log>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Logging;
|
||||
|
||||
@@ -10,9 +10,9 @@ namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
||||
[TestClass]
|
||||
public class LoggerResolverTests
|
||||
{
|
||||
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
|
||||
private readonly Mock<ILoggerFactory> loggerFactoryMock = new();
|
||||
private readonly Mock<ILogger> loggerMock = new();
|
||||
private readonly Slim.IServiceProvider serviceProviderMock = Substitute.For<Slim.IServiceProvider>();
|
||||
private readonly ILoggerFactory loggerFactoryMock = Substitute.For<ILoggerFactory>();
|
||||
private readonly ILogger loggerMock = Substitute.For<ILogger>();
|
||||
private readonly LoggerResolver loggerResolver = new();
|
||||
|
||||
[TestMethod]
|
||||
@@ -53,7 +53,7 @@ public class LoggerResolverTests
|
||||
{
|
||||
this.SetupIServiceProvider();
|
||||
|
||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(ILogger));
|
||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock, typeof(ILogger));
|
||||
|
||||
logger.Should().BeAssignableTo<ILogger>();
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class LoggerResolverTests
|
||||
{
|
||||
this.SetupIServiceProvider();
|
||||
|
||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(ILogger<string>));
|
||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock, typeof(ILogger<string>));
|
||||
|
||||
logger.Should().BeAssignableTo<ILogger<string>>();
|
||||
}
|
||||
@@ -75,7 +75,7 @@ public class LoggerResolverTests
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
|
||||
this.loggerResolver.Resolve(this.serviceProviderMock, typeof(string));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
@@ -88,7 +88,7 @@ public class LoggerResolverTests
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(ILogger<>));
|
||||
this.loggerResolver.Resolve(this.serviceProviderMock, typeof(ILogger<>));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
@@ -96,12 +96,8 @@ public class LoggerResolverTests
|
||||
|
||||
private void SetupIServiceProvider()
|
||||
{
|
||||
this.serviceProviderMock
|
||||
.Setup(u => u.GetService<ILoggerFactory>())
|
||||
.Returns(this.loggerFactoryMock.Object);
|
||||
this.serviceProviderMock.GetService<ILoggerFactory>().Returns(this.loggerFactoryMock);
|
||||
|
||||
this.loggerFactoryMock
|
||||
.Setup(u => u.CreateLogger(It.IsAny<string>()))
|
||||
.Returns(this.loggerMock.Object);
|
||||
this.loggerFactoryMock.CreateLogger(Arg.Any<string>()).Returns(this.loggerMock);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user