Skip to content

Commit

Permalink
- replace moq with nSubstitute
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonG96 committed Aug 9, 2023
1 parent 123c4d1 commit 3ee8910
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 60 deletions.
4 changes: 2 additions & 2 deletions LightweightIocContainer.Validation/IocValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Interfaces.Registrations.Fluent;
using Moq;
using NSubstitute;

namespace LightweightIocContainer.Validation;

Expand Down Expand Up @@ -79,7 +79,7 @@ private void TryResolve(Type type, object?[]? arguments, List<Exception> validat
validationExceptions.Add(exception);
}

private T GetMock<T>() where T : class => new Mock<T>().Object;
private T GetMock<T>() where T : class => Substitute.For<T>();
private object? GetMockOrDefault(Type type)
{
if (type.IsValueType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
</ItemGroup>

Expand Down
14 changes: 7 additions & 7 deletions Test.LightweightIocContainer.Validation/IocValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Validation;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer.Validation;
Expand Down Expand Up @@ -152,14 +152,14 @@ public void TestValidateWithParameter()

IocValidator validator = new(iocContainer);

Mock<IParameter> parameterMock = new();
parameterMock.Setup(p => p.Method()).Returns(true);
IParameter parameterMock = Substitute.For<IParameter>();
parameterMock.Method().Returns(true);

validator.AddParameter<ITest, IParameter>(parameterMock.Object);
validator.AddParameter<ITest, IParameter>(parameterMock);

validator.Validate();

parameterMock.Verify(p => p.Method(), Times.Never);
parameterMock.DidNotReceive().Method();
}

[Test]
Expand All @@ -181,8 +181,8 @@ public void TestValidateWithInvalidParameterWithFactory()

IocValidator validator = new(iocContainer);

Mock<IParameter> parameterMock = new();
validator.AddParameter<ITest, IParameter>(parameterMock.Object);
IParameter parameterMock = Substitute.For<IParameter>();
validator.AddParameter<ITest, IParameter>(parameterMock);

AggregateException aggregateException = Assert.Throws<AggregateException>(() => validator.Validate());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
22 changes: 11 additions & 11 deletions Test.LightweightIocContainer/AssemblyInstallerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer;
Expand All @@ -20,7 +20,7 @@ public class AssemblyInstallerTest
[UsedImplicitly]
public class TestInstaller : IIocInstaller
{
public void Install(IRegistrationCollector registration) => registration.Add<Mock<IRegistration>>();
public void Install(IRegistrationCollector registration) => registration.Add<IRegistration>();
}

[UsedImplicitly]
Expand All @@ -39,15 +39,15 @@ public void TestInstall()
typeof(TestInstaller)
};

Mock<AssemblyWrapper> assemblyMock = new();
assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray);
AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>();
assemblyMock.GetTypes().Returns(types.ToArray());

Mock<IRegistrationCollector> registrationCollectorMock = new();
IRegistrationCollector registrationCollectorMock = Substitute.For<IRegistrationCollector>();

AssemblyInstaller assemblyInstaller = new(assemblyMock.Object);
assemblyInstaller.Install(registrationCollectorMock.Object);
AssemblyInstaller assemblyInstaller = new(assemblyMock);
assemblyInstaller.Install(registrationCollectorMock);

registrationCollectorMock.Verify(r => r.Add<It.IsSubtype<Mock<IRegistration>>>(It.IsAny<Lifestyle>()), Times.Once);
registrationCollectorMock.Received(1).Add<IRegistration>(Arg.Any<Lifestyle>());
}

[Test]
Expand All @@ -66,10 +66,10 @@ public void TestFromAssemblyInstance()
typeof(TestInstaller)
};

Mock<AssemblyWrapper> assemblyMock = new();
assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray);
AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>();
assemblyMock.GetTypes().Returns(types.ToArray());

IIocContainer iocContainer = new IocContainer();
iocContainer.Install(FromAssembly.Instance(assemblyMock.Object));
iocContainer.Install(FromAssembly.Instance(assemblyMock));
}
}
20 changes: 10 additions & 10 deletions Test.LightweightIocContainer/EnumerableExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using LightweightIocContainer;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer;
Expand Down Expand Up @@ -83,18 +83,18 @@ public void TestFirstOrGivenPredicate()
[Test]
public void TestForEach()
{
Mock<ITest> test1 = new();
Mock<ITest> test2 = new();
Mock<ITest> test3 = new();
Mock<ITest> test4 = new();
ITest test1 = Substitute.For<ITest>();
ITest test2 = Substitute.For<ITest>();
ITest test3 = Substitute.For<ITest>();
ITest test4 = Substitute.For<ITest>();

IEnumerable<ITest> enumerable = new[] { test1.Object, test2.Object, test3.Object, test4.Object };
IEnumerable<ITest> enumerable = new[] { test1, test2, test3, test4 };

enumerable.ForEach(t => t.DoSomething());

test1.Verify(t => t.DoSomething(), Times.Once);
test2.Verify(t => t.DoSomething(), Times.Once);
test3.Verify(t => t.DoSomething(), Times.Once);
test4.Verify(t => t.DoSomething(), Times.Once);
test1.Received(1).DoSomething();
test2.Received(1).DoSomething();
test3.Received(1).DoSomething();
test4.Received(1).DoSomething();
}
}
6 changes: 3 additions & 3 deletions Test.LightweightIocContainer/IocContainerRecursionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using JetBrains.Annotations;
using LightweightIocContainer;
using LightweightIocContainer.Exceptions;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer;
Expand Down Expand Up @@ -148,8 +148,8 @@ public void TestRecursionWithParam()
_iocContainer.Register(r => r.Add<IFoo, Foo>());
_iocContainer.Register(r => r.Add<IBar, Bar>());

Assert.DoesNotThrow(() => _iocContainer.Resolve<IFoo>(new Mock<IBar>().Object));
Assert.DoesNotThrow(() => _iocContainer.Resolve<IBar>(new Mock<IFoo>().Object));
Assert.DoesNotThrow(() => _iocContainer.Resolve<IFoo>(Substitute.For<IBar>()));
Assert.DoesNotThrow(() => _iocContainer.Resolve<IBar>(Substitute.For<IFoo>()));
}

[Test]
Expand Down
22 changes: 11 additions & 11 deletions Test.LightweightIocContainer/IocContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer;
Expand Down Expand Up @@ -119,26 +119,26 @@ private class MultitonScope
[Test]
public void TestInstall()
{
Mock<IIocInstaller> installerMock = new();
IIocContainer returnedContainer = _iocContainer.Install(installerMock.Object);
IIocInstaller installerMock = Substitute.For<IIocInstaller>();
IIocContainer returnedContainer = _iocContainer.Install(installerMock);

installerMock.Verify(m => m.Install(It.IsAny<IRegistrationCollector>()), Times.Once);
installerMock.Received(1).Install(Arg.Any<IRegistrationCollector>());

Assert.AreEqual(_iocContainer, returnedContainer);
}

[Test]
public void TestInstallMultiple()
{
Mock<IIocInstaller> installer1Mock = new();
Mock<IIocInstaller> installer2Mock = new();
Mock<IIocInstaller> installer3Mock = new();
IIocInstaller installer1Mock = Substitute.For<IIocInstaller>();
IIocInstaller installer2Mock = Substitute.For<IIocInstaller>();
IIocInstaller installer3Mock = Substitute.For<IIocInstaller>();

IIocContainer returnedContainer = _iocContainer.Install(installer1Mock.Object, installer2Mock.Object, installer3Mock.Object);
IIocContainer returnedContainer = _iocContainer.Install(installer1Mock, installer2Mock, installer3Mock);

installer1Mock.Verify(m => m.Install(It.IsAny<IRegistrationCollector>()), Times.Once);
installer2Mock.Verify(m => m.Install(It.IsAny<IRegistrationCollector>()), Times.Once);
installer3Mock.Verify(m => m.Install(It.IsAny<IRegistrationCollector>()), Times.Once);
installer1Mock.Received(1).Install(Arg.Any<IRegistrationCollector>());
installer2Mock.Received(1).Install(Arg.Any<IRegistrationCollector>());
installer3Mock.Received(1).Install(Arg.Any<IRegistrationCollector>());

Assert.AreEqual(_iocContainer, returnedContainer);
}
Expand Down
4 changes: 2 additions & 2 deletions Test.LightweightIocContainer/OnCreateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using LightweightIocContainer;
using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Registrations;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer;
Expand All @@ -27,7 +27,7 @@ private class Test : ITest
[Test]
public void TestOnCreate()
{
RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
RegistrationFactory registrationFactory = new(Substitute.For<IocContainer>());
ITypedRegistration<ITest, Test> testRegistration = registrationFactory.Register<ITest, Test>(Lifestyle.Transient).OnCreate(t => t.DoSomething());

Test test = new();
Expand Down
10 changes: 5 additions & 5 deletions Test.LightweightIocContainer/RegistrationBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Registrations;
using LightweightIocContainer.ResolvePlaceholders;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer;
Expand Down Expand Up @@ -53,7 +53,7 @@ private class Bar : IBar
[Test]
public void TestWithParameters()
{
RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
RegistrationFactory registrationFactory = new(Substitute.For<IocContainer>());

IBar bar = new Bar();
ITest test = new Test();
Expand All @@ -67,7 +67,7 @@ public void TestWithParameters()
[Test]
public void TestWithParametersDifferentOrder()
{
RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
RegistrationFactory registrationFactory = new(Substitute.For<IocContainer>());

IBar bar = new Bar();
ITest test = new Test();
Expand All @@ -83,15 +83,15 @@ public void TestWithParametersDifferentOrder()
[Test]
public void TestWithParametersCalledTwice()
{
RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
RegistrationFactory registrationFactory = new(Substitute.For<IocContainer>());
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(new Bar()).WithParameters(new Test()));
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((0, new Bar())).WithParameters((1, new Test())));
}

[Test]
public void TestWithParametersNoParametersGiven()
{
RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
RegistrationFactory registrationFactory = new(Substitute.For<IocContainer>());
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((object[])null));
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(((int index, object parameter)[])null));
}
Expand Down
13 changes: 7 additions & 6 deletions Test.LightweightIocContainer/SingleTypeRegistrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using LightweightIocContainer;
using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Registrations;
using Moq;
using NSubstitute;
using NUnit.Framework;

namespace Test.LightweightIocContainer;
Expand All @@ -20,7 +20,8 @@ private interface IFoo
IBar Bar { get; }
}

private interface IBar
[UsedImplicitly]
public interface IBar
{

}
Expand All @@ -44,13 +45,13 @@ public void TestSingleTypeRegistrationWithFactoryMethod()
{
IBar bar = new Bar();

Mock<IocContainer> iocContainerMock = new();
iocContainerMock.Setup(c => c.Resolve<IBar>()).Returns(bar);
IocContainer iocContainerMock = Substitute.For<IocContainer>();
iocContainerMock.Resolve<IBar>().Returns(bar);

RegistrationFactory registrationFactory = new(iocContainerMock.Object);
RegistrationFactory registrationFactory = new(iocContainerMock);
ISingleTypeRegistration<IFoo> registration = registrationFactory.Register<IFoo>(Lifestyle.Transient).WithFactoryMethod(c => new Foo(c.Resolve<IBar>()));

IFoo foo = registration.FactoryMethod!(iocContainerMock.Object);
IFoo foo = registration.FactoryMethod!(iocContainerMock);
Assert.AreEqual(bar, foo.Bar);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 3ee8910

Please sign in to comment.