Skip to content

Commit

Permalink
chore(deps): replace Moq with NSubstitute
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer committed Aug 10, 2023
1 parent 4ade9ff commit 7d42871
Show file tree
Hide file tree
Showing 27 changed files with 205 additions and 220 deletions.
8 changes: 6 additions & 2 deletions test/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

<ItemGroup>
<Using Include="FluentAssertions" />
<Using Include="Moq" />
<Using Include="NSubstitute" />
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="*" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.2" />
</ItemGroup>

Expand Down
48 changes: 23 additions & 25 deletions test/IbanNet.DataAnnotations.Tests/IbanAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ namespace IbanNet.DataAnnotations;
[Collection(nameof(SetsStaticValidator))]
public class IbanAttributeTests
{
private readonly Mock<IServiceProvider> _serviceProviderMock;
private readonly IServiceProvider _serviceProviderMock;
private readonly IbanAttribute _sut;
private readonly IbanValidatorStub _ibanValidatorStub;
private readonly IIbanValidator _ibanValidatorStub;

private ValidationContext _validationContext;

protected IbanAttributeTests()
{
_ibanValidatorStub = new IbanValidatorStub();
_serviceProviderMock = new Mock<IServiceProvider>();
_ibanValidatorStub = IbanValidatorStub.Create();
_serviceProviderMock = Substitute.For<IServiceProvider>();
_serviceProviderMock
.Setup(m => m.GetService(typeof(IIbanValidator)))
.GetService(typeof(IIbanValidator))
.Returns(_ibanValidatorStub);

_validationContext = new ValidationContext(new object(), _serviceProviderMock.Object, null);
_validationContext = new ValidationContext(new object(), _serviceProviderMock, null);

_sut = new IbanAttribute();
}
Expand All @@ -35,7 +35,7 @@ public void It_should_not_call_validator()
_sut.GetValidationResult(null, _validationContext);

// Assert
_ibanValidatorStub.VerifyNoOtherCalls();
_ibanValidatorStub.ReceivedCalls().Should().BeEmpty();
}

[Fact]
Expand All @@ -45,7 +45,7 @@ public void It_should_not_resolve_the_validator()
_sut.GetValidationResult(null, _validationContext);

// Assert
_serviceProviderMock.Verify(m => m.GetService(It.IsAny<Type>()), Times.Never);
_serviceProviderMock.DidNotReceiveWithAnyArgs().GetService(default!);
}

[Fact]
Expand Down Expand Up @@ -78,8 +78,8 @@ public void It_should_call_validator()
_sut.GetValidationResult(TestValues.ValidIban, _validationContext);

// Assert
_ibanValidatorStub.Verify(m => m.Validate(TestValues.ValidIban), Times.Once);
_ibanValidatorStub.VerifyNoOtherCalls();
_ibanValidatorStub.Received(1).Validate(TestValues.ValidIban);
_ibanValidatorStub.ReceivedCalls().Should().HaveCount(1);
}

[Fact]
Expand All @@ -89,8 +89,8 @@ public void It_should_resolve_the_validator()
_sut.GetValidationResult(TestValues.ValidIban, _validationContext);

// Assert
_serviceProviderMock.Verify(m => m.GetService(typeof(IIbanValidator)));
_serviceProviderMock.VerifyNoOtherCalls();
_serviceProviderMock.Received(1).GetService(typeof(IIbanValidator));
_serviceProviderMock.ReceivedCalls().Should().HaveCount(1);
}

[Fact]
Expand Down Expand Up @@ -123,8 +123,8 @@ public void It_should_call_validator()
_sut.GetValidationResult(TestValues.InvalidIban, _validationContext);

// Assert
_ibanValidatorStub.Verify(m => m.Validate(TestValues.InvalidIban), Times.Once);
_ibanValidatorStub.VerifyNoOtherCalls();
_ibanValidatorStub.Received(1).Validate(TestValues.InvalidIban);
_ibanValidatorStub.ReceivedCalls().Should().HaveCount(1);
}

[Fact]
Expand Down Expand Up @@ -156,8 +156,8 @@ public void It_should_resolve_the_validator()
_sut.GetValidationResult(TestValues.InvalidIban, _validationContext);

// Assert
_serviceProviderMock.Verify(m => m.GetService(typeof(IIbanValidator)));
_serviceProviderMock.VerifyNoOtherCalls();
_serviceProviderMock.Received(1).GetService(typeof(IIbanValidator));
_serviceProviderMock.ReceivedCalls().Should().HaveCount(1);
}

[Fact]
Expand Down Expand Up @@ -215,8 +215,8 @@ public void It_should_throw()
// Assert
act.Should().Throw<NotImplementedException>();

_serviceProviderMock.Verify(m => m.GetService(It.IsAny<Type>()), Times.Never);
_ibanValidatorStub.VerifyNoOtherCalls();
_serviceProviderMock.DidNotReceiveWithAnyArgs().GetService(default!);
_ibanValidatorStub.ReceivedCalls().Should().BeEmpty();
}
}

Expand All @@ -227,9 +227,8 @@ public void It_should_throw()
{
// Arrange
_serviceProviderMock
.Setup(m => m.GetService(typeof(IIbanValidator)))
.Returns(null)
.Verifiable();
.GetService(Arg.Any<Type>())
.Returns(null);

// Act
Action act = () => _sut.GetValidationResult(TestValues.ValidIban, _validationContext);
Expand All @@ -238,8 +237,8 @@ public void It_should_throw()
act.Should()
.Throw<InvalidOperationException>()
.WithMessage("Failed to get an instance of *");
_serviceProviderMock.Verify();
_ibanValidatorStub.VerifyNoOtherCalls();
_serviceProviderMock.Received(1).GetService(typeof(IIbanValidator));
_ibanValidatorStub.ReceivedCalls().Should().BeEmpty();
}
}

Expand All @@ -258,8 +257,7 @@ public void It_should_throw()
act.Should()
.Throw<InvalidOperationException>()
.WithMessage("Failed to get an instance of *");
_ibanValidatorStub.VerifyNoOtherCalls();
}
_ibanValidatorStub.ReceivedCalls().Should().BeEmpty(); }
}

public class Requires_validation_context : IbanAttributeTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public CustomServicesDependencyInjectionFixture() : base(false)

protected override void Configure(ContainerBuilder containerBuilder, Action<IIbanNetOptionsBuilder> configurer)
{
containerBuilder.RegisterInstance(Mock.Of<IIbanRegistry>());
containerBuilder.RegisterInstance(Mock.Of<IIbanParser>());
containerBuilder.RegisterInstance(Mock.Of<IIbanGenerator>());
containerBuilder.RegisterInstance(Mock.Of<IIbanValidator>());
containerBuilder.RegisterInstance(Substitute.For<IIbanRegistry>());
containerBuilder.RegisterInstance(Substitute.For<IIbanParser>());
containerBuilder.RegisterInstance(Substitute.For<IIbanGenerator>());
containerBuilder.RegisterInstance(Substitute.For<IIbanValidator>());

base.Configure(containerBuilder, configurer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public CustomServicesDependencyInjectionFixture() : base(false)

protected override void Configure(IServiceCollection services, Action<IIbanNetOptionsBuilder> configurer)
{
services.AddSingleton(Mock.Of<IIbanRegistry>());
services.AddSingleton(Mock.Of<IIbanParser>());
services.AddSingleton(Mock.Of<IIbanGenerator>());
services.AddSingleton(Mock.Of<IIbanValidator>());
services.AddSingleton(Substitute.For<IIbanRegistry>());
services.AddSingleton(Substitute.For<IIbanParser>());
services.AddSingleton(Substitute.For<IIbanGenerator>());
services.AddSingleton(Substitute.For<IIbanValidator>());

base.Configure(services, configurer);
}
Expand Down
10 changes: 5 additions & 5 deletions test/IbanNet.FluentValidation.Tests/FluentIbanValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace IbanNet.FluentValidation;
[Collection(nameof(SetsStaticValidator))]
public class FluentIbanValidatorTests
{
private readonly IbanValidatorStub _ibanValidatorStub;
private readonly IIbanValidator _ibanValidatorStub;
private readonly TestModelValidator _validator;

protected FluentIbanValidatorTests()
{
_ibanValidatorStub = new IbanValidatorStub();
_ibanValidatorStub = IbanValidatorStub.Create();
var sut = new FluentIbanValidator<TestModel>(_ibanValidatorStub);
_validator = new TestModelValidator(sut);
}
Expand All @@ -32,7 +32,7 @@ public void It_should_call_validator()
_validator.Validate(obj);

// Assert
_ibanValidatorStub.Verify(m => m.Validate(AttemptedIbanValue), Times.Once);
_ibanValidatorStub.Received(1).Validate(AttemptedIbanValue);
}

[Fact]
Expand Down Expand Up @@ -72,7 +72,7 @@ public void It_should_call_validator()
_validator.Validate(obj);

// Assert
_ibanValidatorStub.Verify(m => m.Validate(AttemptedIbanValue), Times.Once);
_ibanValidatorStub.Received(1).Validate(AttemptedIbanValue);
}

[Fact]
Expand Down Expand Up @@ -100,7 +100,7 @@ public void It_should_not_call_validator()
_validator.Validate(obj);

// Assert
_ibanValidatorStub.Verify(m => m.Validate(It.IsAny<string>()), Times.Never);
_ibanValidatorStub.DidNotReceiveWithAnyArgs().Validate(default);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void Given_null_builder_when_registering_validator_it_should_throw()
IRuleBuilder<object, string>? ruleBuilder = null;

// Act
Action act = () => ruleBuilder!.Iban(Mock.Of<IIbanValidator>());
Action act = () => ruleBuilder!.Iban(Substitute.For<IIbanValidator>());

// Assert
act.Should()
Expand All @@ -22,7 +22,7 @@ public void Given_null_builder_when_registering_validator_it_should_throw()
[Fact]
public void Given_null_validator_when_registering_validator_it_should_throw()
{
IRuleBuilder<object, string> ruleBuilder = Mock.Of<IRuleBuilder<object, string>>();
IRuleBuilder<object, string> ruleBuilder = Substitute.For<IRuleBuilder<object, string>>();
IIbanValidator? ibanValidator = null;

// Act
Expand All @@ -37,13 +37,13 @@ public void Given_null_validator_when_registering_validator_it_should_throw()
[Fact]
public void When_registering_validator_it_should_add_validator_to_rule()
{
var ruleBuilderMock = new Mock<IRuleBuilder<object, string>>();
IIbanValidator ibanValidator = Mock.Of<IIbanValidator>();
IRuleBuilder<object, string>? ruleBuilderMock = Substitute.For<IRuleBuilder<object, string>>();
IIbanValidator ibanValidator = Substitute.For<IIbanValidator>();

// Act
ruleBuilderMock.Object.Iban(ibanValidator);
ruleBuilderMock.Iban(ibanValidator);

// Assert
ruleBuilderMock.Verify(m => m.SetValidator(It.Is<IPropertyValidator<object, string>>(x => x is FluentIbanValidator<object>)), Times.Once);
ruleBuilderMock.Received(1).SetValidator(Arg.Is<IPropertyValidator<object, string>>(x => x is FluentIbanValidator<object>));
}
}
15 changes: 8 additions & 7 deletions test/IbanNet.Tests/Builders/BuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ public void When_getting_builder_with_countryCode_it_should_configure_builder(st

public static IEnumerable<object?[]> WithCountryTestCases()
{
IBankAccountBuilder builder = Mock.Of<IBankAccountBuilder>();
const string countryCode = "NL";
IIbanRegistry registry = Mock.Of<IIbanRegistry>();
IBankAccountBuilder builder = Substitute.For<IBankAccountBuilder>();
IIbanRegistry registry = Substitute.For<IIbanRegistry>();

IbanCountry? other = null;
var nl = new IbanCountry(countryCode);
Mock.Get(registry).Setup(m => m.TryGetValue(It.IsAny<string>(), out other));
Mock.Get(registry).Setup(m => m.TryGetValue("NL", out nl));
const string countryCode = "NL";
registry.TryGetValue(Arg.Any<string>(), out Arg.Any<IbanCountry?>()).Returns(x =>
{
x[1] = x.Arg<string>() == countryCode ? new IbanCountry(countryCode) : null;
return false;
});

yield return new object?[] { null, countryCode, registry, nameof(builder) };
yield return new object?[] { builder, null, registry, nameof(countryCode) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace IbanNet.DependencyInjection;
public class DependencyResolverAdapterTests
{
private readonly DependencyResolverAdapter _sut;
private readonly Mock<DependencyResolverAdapter> _adapterStub;

private class TestService
{
Expand All @@ -15,48 +14,45 @@ private class TestService
public DependencyResolverAdapterTests()
{
_sut = CreateAdapterStub();
_adapterStub = Mock.Get(_sut);
}

private static DependencyResolverAdapter CreateAdapterStub()
{
var adapterStub = new Mock<DependencyResolverAdapter> { CallBase = true };
DependencyResolverAdapter adapterStub = Substitute.For<DependencyResolverAdapter>();
adapterStub
.Setup(m => m.GetService(It.IsAny<Type>()))
.Returns<Type>(Activator.CreateInstance);
return adapterStub.Object;
.GetService(Arg.Any<Type>())
.Returns(info => Activator.CreateInstance(info.Arg<Type>()));
return adapterStub;
}

[Fact]
public void Given_service_is_not_registered_when_getting_generic_required_it_should_throw()
{
_adapterStub
.Setup(m => m.GetService(It.IsAny<Type>()))
.Returns(null)
.Verifiable();
_sut
.GetService(Arg.Any<Type>())
.Returns(null);

// Act
Action act = () => _sut.GetRequiredService<TestService>();

// Assert
act.Should().Throw<InvalidOperationException>();
_adapterStub.Verify();
_sut.Received(1).GetService(Arg.Any<Type>());
}

[Fact]
public void Given_service_is_not_registered_when_getting_required_it_should_throw()
{
_adapterStub
.Setup(m => m.GetService(It.IsAny<Type>()))
.Returns(null)
.Verifiable();
_sut
.GetService(Arg.Any<Type>())
.Returns(null);

// Act
Action act = () => _sut.GetRequiredService(typeof(TestService));

// Assert
act.Should().Throw<InvalidOperationException>();
_adapterStub.Verify();
_sut.Received(1).GetService(Arg.Any<Type>());
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace IbanNet.DependencyInjection.FluentAssertions;

public class IbanNetOptionsBuilderStubAssertions<T>
: ReferenceTypeAssertions<Mock<T>, IbanNetOptionsBuilderStubAssertions<T>>
: ReferenceTypeAssertions<T, IbanNetOptionsBuilderStubAssertions<T>>
where T : class, IIbanNetOptionsBuilder
{
public IbanNetOptionsBuilderStubAssertions(Mock<T> instance)
public IbanNetOptionsBuilderStubAssertions(T instance)
: base(instance) { }

protected override string Identifier => typeof(T).Name;
Expand Down Expand Up @@ -46,13 +46,13 @@ private void VerifyCalled(Action<IbanValidatorOptionsAssertions> should)
Func<Action<DependencyResolverAdapter, IbanValidatorOptions>, bool> assert = configureAction =>
{
var opts = new IbanValidatorOptions();
DependencyResolverAdapter adapter = new Mock<DependencyResolverAdapter>().Object;
DependencyResolverAdapter adapter = Substitute.For<DependencyResolverAdapter>();
configureAction(adapter, opts);
should(opts.Should());
return true;
};

Subject.Verify(m => m.Configure(It.Is<Action<DependencyResolverAdapter, IbanValidatorOptions>>(action => assert(action))));
Subject.Received().Configure(Arg.Is<Action<DependencyResolverAdapter, IbanValidatorOptions>>(action => assert(action)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static class IbanNetOptionsBuilderStubExtensions
{
public static IbanNetOptionsBuilderStubAssertions<T> Should<T>(this Mock<T> instance)
public static IbanNetOptionsBuilderStubAssertions<T> Should<T>(this T instance)
where T : class, IIbanNetOptionsBuilder
{
return new(instance);
Expand Down
Loading

0 comments on commit 7d42871

Please sign in to comment.