From 7d428719c3c9dee00370ed1f12238177e34d2c46 Mon Sep 17 00:00:00 2001 From: skwasjer <11424653+skwasjer@users.noreply.github.com> Date: Thu, 10 Aug 2023 06:41:53 +0200 Subject: [PATCH] chore(deps): replace Moq with NSubstitute --- test/Directory.Build.targets | 8 +++- .../IbanAttributeTests.cs | 48 +++++++++---------- .../Specs/ShouldResolveCustomService.cs | 8 ++-- .../Specs/ShouldResolveCustomService.cs | 8 ++-- .../FluentIbanValidatorTests.cs | 10 ++-- .../RuleBuilderExtensionsTests.cs | 12 ++--- .../Builders/BuilderExtensionsTests.cs | 15 +++--- .../DependencyResolverAdapterTests.cs | 28 +++++------ .../IbanNetOptionsBuilderStubAssertions.cs | 8 ++-- .../IbanNetOptionsBuilderStubExtensions.cs | 2 +- .../IbanNetOptionsBuilderTests.cs | 34 +++++++------ test/IbanNet.Tests/IbanParserTests.cs | 17 ++++--- test/IbanNet.Tests/IbanTests.cs | 4 +- test/IbanNet.Tests/IbanValidatorTests.cs | 21 ++++---- .../Json/IbanJsonConverterTests.cs | 18 ++++--- .../Registry/IbanRegistryTests.cs | 6 +-- .../Registry/Patterns/PatternTests.cs | 25 +++++----- .../TypeConverters/IbanTypeConverterTests.cs | 31 ++++++------ .../DefaultValidationRuleResolverTests.cs | 10 ++-- .../Validation/Rules/Mod97RuleTests.cs | 22 ++++----- .../FluentAssertions/DelegateAssertions.cs | 6 +-- test/TestHelpers/IbanValidatorStub.cs | 38 +++++++-------- .../Specs/ConfiguredMultipleRulesSpec.cs | 2 +- .../ShouldPreserveStaticValidatorSpec.cs | 2 +- .../Specs/ShouldResolveCustomService.cs | 34 ++++++------- .../Specs/ShouldSetStaticValidatorSpec.cs | 2 +- test/TestHelpers/TestValues.cs | 6 +-- 27 files changed, 205 insertions(+), 220 deletions(-) diff --git a/test/Directory.Build.targets b/test/Directory.Build.targets index 37a31ea8..72757035 100644 --- a/test/Directory.Build.targets +++ b/test/Directory.Build.targets @@ -8,13 +8,17 @@ - + - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/IbanNet.DataAnnotations.Tests/IbanAttributeTests.cs b/test/IbanNet.DataAnnotations.Tests/IbanAttributeTests.cs index 2f607da5..9a985b7f 100644 --- a/test/IbanNet.DataAnnotations.Tests/IbanAttributeTests.cs +++ b/test/IbanNet.DataAnnotations.Tests/IbanAttributeTests.cs @@ -7,21 +7,21 @@ namespace IbanNet.DataAnnotations; [Collection(nameof(SetsStaticValidator))] public class IbanAttributeTests { - private readonly Mock _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(); + _ibanValidatorStub = IbanValidatorStub.Create(); + _serviceProviderMock = Substitute.For(); _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(); } @@ -35,7 +35,7 @@ public void It_should_not_call_validator() _sut.GetValidationResult(null, _validationContext); // Assert - _ibanValidatorStub.VerifyNoOtherCalls(); + _ibanValidatorStub.ReceivedCalls().Should().BeEmpty(); } [Fact] @@ -45,7 +45,7 @@ public void It_should_not_resolve_the_validator() _sut.GetValidationResult(null, _validationContext); // Assert - _serviceProviderMock.Verify(m => m.GetService(It.IsAny()), Times.Never); + _serviceProviderMock.DidNotReceiveWithAnyArgs().GetService(default!); } [Fact] @@ -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] @@ -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] @@ -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] @@ -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] @@ -215,8 +215,8 @@ public void It_should_throw() // Assert act.Should().Throw(); - _serviceProviderMock.Verify(m => m.GetService(It.IsAny()), Times.Never); - _ibanValidatorStub.VerifyNoOtherCalls(); + _serviceProviderMock.DidNotReceiveWithAnyArgs().GetService(default!); + _ibanValidatorStub.ReceivedCalls().Should().BeEmpty(); } } @@ -227,9 +227,8 @@ public void It_should_throw() { // Arrange _serviceProviderMock - .Setup(m => m.GetService(typeof(IIbanValidator))) - .Returns(null) - .Verifiable(); + .GetService(Arg.Any()) + .Returns(null); // Act Action act = () => _sut.GetValidationResult(TestValues.ValidIban, _validationContext); @@ -238,8 +237,8 @@ public void It_should_throw() act.Should() .Throw() .WithMessage("Failed to get an instance of *"); - _serviceProviderMock.Verify(); - _ibanValidatorStub.VerifyNoOtherCalls(); + _serviceProviderMock.Received(1).GetService(typeof(IIbanValidator)); + _ibanValidatorStub.ReceivedCalls().Should().BeEmpty(); } } @@ -258,8 +257,7 @@ public void It_should_throw() act.Should() .Throw() .WithMessage("Failed to get an instance of *"); - _ibanValidatorStub.VerifyNoOtherCalls(); - } + _ibanValidatorStub.ReceivedCalls().Should().BeEmpty(); } } public class Requires_validation_context : IbanAttributeTests diff --git a/test/IbanNet.DependencyInjection.Autofac.Tests/Specs/ShouldResolveCustomService.cs b/test/IbanNet.DependencyInjection.Autofac.Tests/Specs/ShouldResolveCustomService.cs index eca26430..e3adbbd0 100644 --- a/test/IbanNet.DependencyInjection.Autofac.Tests/Specs/ShouldResolveCustomService.cs +++ b/test/IbanNet.DependencyInjection.Autofac.Tests/Specs/ShouldResolveCustomService.cs @@ -19,10 +19,10 @@ public CustomServicesDependencyInjectionFixture() : base(false) protected override void Configure(ContainerBuilder containerBuilder, Action configurer) { - containerBuilder.RegisterInstance(Mock.Of()); - containerBuilder.RegisterInstance(Mock.Of()); - containerBuilder.RegisterInstance(Mock.Of()); - containerBuilder.RegisterInstance(Mock.Of()); + containerBuilder.RegisterInstance(Substitute.For()); + containerBuilder.RegisterInstance(Substitute.For()); + containerBuilder.RegisterInstance(Substitute.For()); + containerBuilder.RegisterInstance(Substitute.For()); base.Configure(containerBuilder, configurer); } diff --git a/test/IbanNet.DependencyInjection.ServiceProvider.Tests/Specs/ShouldResolveCustomService.cs b/test/IbanNet.DependencyInjection.ServiceProvider.Tests/Specs/ShouldResolveCustomService.cs index 493d81bb..4ca4800d 100644 --- a/test/IbanNet.DependencyInjection.ServiceProvider.Tests/Specs/ShouldResolveCustomService.cs +++ b/test/IbanNet.DependencyInjection.ServiceProvider.Tests/Specs/ShouldResolveCustomService.cs @@ -19,10 +19,10 @@ public CustomServicesDependencyInjectionFixture() : base(false) protected override void Configure(IServiceCollection services, Action configurer) { - services.AddSingleton(Mock.Of()); - services.AddSingleton(Mock.Of()); - services.AddSingleton(Mock.Of()); - services.AddSingleton(Mock.Of()); + services.AddSingleton(Substitute.For()); + services.AddSingleton(Substitute.For()); + services.AddSingleton(Substitute.For()); + services.AddSingleton(Substitute.For()); base.Configure(services, configurer); } diff --git a/test/IbanNet.FluentValidation.Tests/FluentIbanValidatorTests.cs b/test/IbanNet.FluentValidation.Tests/FluentIbanValidatorTests.cs index 61ab8d1f..0f97c125 100644 --- a/test/IbanNet.FluentValidation.Tests/FluentIbanValidatorTests.cs +++ b/test/IbanNet.FluentValidation.Tests/FluentIbanValidatorTests.cs @@ -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(_ibanValidatorStub); _validator = new TestModelValidator(sut); } @@ -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] @@ -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] @@ -100,7 +100,7 @@ public void It_should_not_call_validator() _validator.Validate(obj); // Assert - _ibanValidatorStub.Verify(m => m.Validate(It.IsAny()), Times.Never); + _ibanValidatorStub.DidNotReceiveWithAnyArgs().Validate(default); } [Fact] diff --git a/test/IbanNet.FluentValidation.Tests/RuleBuilderExtensionsTests.cs b/test/IbanNet.FluentValidation.Tests/RuleBuilderExtensionsTests.cs index ea9303a3..46bf1c75 100644 --- a/test/IbanNet.FluentValidation.Tests/RuleBuilderExtensionsTests.cs +++ b/test/IbanNet.FluentValidation.Tests/RuleBuilderExtensionsTests.cs @@ -11,7 +11,7 @@ public void Given_null_builder_when_registering_validator_it_should_throw() IRuleBuilder? ruleBuilder = null; // Act - Action act = () => ruleBuilder!.Iban(Mock.Of()); + Action act = () => ruleBuilder!.Iban(Substitute.For()); // Assert act.Should() @@ -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 ruleBuilder = Mock.Of>(); + IRuleBuilder ruleBuilder = Substitute.For>(); IIbanValidator? ibanValidator = null; // Act @@ -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>(); - IIbanValidator ibanValidator = Mock.Of(); + IRuleBuilder? ruleBuilderMock = Substitute.For>(); + IIbanValidator ibanValidator = Substitute.For(); // Act - ruleBuilderMock.Object.Iban(ibanValidator); + ruleBuilderMock.Iban(ibanValidator); // Assert - ruleBuilderMock.Verify(m => m.SetValidator(It.Is>(x => x is FluentIbanValidator)), Times.Once); + ruleBuilderMock.Received(1).SetValidator(Arg.Is>(x => x is FluentIbanValidator)); } } diff --git a/test/IbanNet.Tests/Builders/BuilderExtensionsTests.cs b/test/IbanNet.Tests/Builders/BuilderExtensionsTests.cs index 30fedc08..88ad6795 100644 --- a/test/IbanNet.Tests/Builders/BuilderExtensionsTests.cs +++ b/test/IbanNet.Tests/Builders/BuilderExtensionsTests.cs @@ -94,14 +94,15 @@ public void When_getting_builder_with_countryCode_it_should_configure_builder(st public static IEnumerable WithCountryTestCases() { - IBankAccountBuilder builder = Mock.Of(); - const string countryCode = "NL"; - IIbanRegistry registry = Mock.Of(); + IBankAccountBuilder builder = Substitute.For(); + IIbanRegistry registry = Substitute.For(); - IbanCountry? other = null; - var nl = new IbanCountry(countryCode); - Mock.Get(registry).Setup(m => m.TryGetValue(It.IsAny(), out other)); - Mock.Get(registry).Setup(m => m.TryGetValue("NL", out nl)); + const string countryCode = "NL"; + registry.TryGetValue(Arg.Any(), out Arg.Any()).Returns(x => + { + x[1] = x.Arg() == 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) }; diff --git a/test/IbanNet.Tests/DependencyInjection/DependencyResolverAdapterTests.cs b/test/IbanNet.Tests/DependencyInjection/DependencyResolverAdapterTests.cs index db0f44e1..11871b32 100644 --- a/test/IbanNet.Tests/DependencyInjection/DependencyResolverAdapterTests.cs +++ b/test/IbanNet.Tests/DependencyInjection/DependencyResolverAdapterTests.cs @@ -6,7 +6,6 @@ namespace IbanNet.DependencyInjection; public class DependencyResolverAdapterTests { private readonly DependencyResolverAdapter _sut; - private readonly Mock _adapterStub; private class TestService { @@ -15,48 +14,45 @@ private class TestService public DependencyResolverAdapterTests() { _sut = CreateAdapterStub(); - _adapterStub = Mock.Get(_sut); } private static DependencyResolverAdapter CreateAdapterStub() { - var adapterStub = new Mock { CallBase = true }; + DependencyResolverAdapter adapterStub = Substitute.For(); adapterStub - .Setup(m => m.GetService(It.IsAny())) - .Returns(Activator.CreateInstance); - return adapterStub.Object; + .GetService(Arg.Any()) + .Returns(info => Activator.CreateInstance(info.Arg())); + return adapterStub; } [Fact] public void Given_service_is_not_registered_when_getting_generic_required_it_should_throw() { - _adapterStub - .Setup(m => m.GetService(It.IsAny())) - .Returns(null) - .Verifiable(); + _sut + .GetService(Arg.Any()) + .Returns(null); // Act Action act = () => _sut.GetRequiredService(); // Assert act.Should().Throw(); - _adapterStub.Verify(); + _sut.Received(1).GetService(Arg.Any()); } [Fact] public void Given_service_is_not_registered_when_getting_required_it_should_throw() { - _adapterStub - .Setup(m => m.GetService(It.IsAny())) - .Returns(null) - .Verifiable(); + _sut + .GetService(Arg.Any()) + .Returns(null); // Act Action act = () => _sut.GetRequiredService(typeof(TestService)); // Assert act.Should().Throw(); - _adapterStub.Verify(); + _sut.Received(1).GetService(Arg.Any()); } [Theory] diff --git a/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubAssertions.cs b/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubAssertions.cs index 3638b8f1..57bba91a 100644 --- a/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubAssertions.cs +++ b/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubAssertions.cs @@ -7,10 +7,10 @@ namespace IbanNet.DependencyInjection.FluentAssertions; public class IbanNetOptionsBuilderStubAssertions - : ReferenceTypeAssertions, IbanNetOptionsBuilderStubAssertions> + : ReferenceTypeAssertions> where T : class, IIbanNetOptionsBuilder { - public IbanNetOptionsBuilderStubAssertions(Mock instance) + public IbanNetOptionsBuilderStubAssertions(T instance) : base(instance) { } protected override string Identifier => typeof(T).Name; @@ -46,13 +46,13 @@ private void VerifyCalled(Action should) Func, bool> assert = configureAction => { var opts = new IbanValidatorOptions(); - DependencyResolverAdapter adapter = new Mock().Object; + DependencyResolverAdapter adapter = Substitute.For(); configureAction(adapter, opts); should(opts.Should()); return true; }; - Subject.Verify(m => m.Configure(It.Is>(action => assert(action)))); + Subject.Received().Configure(Arg.Is>(action => assert(action))); } } diff --git a/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubExtensions.cs b/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubExtensions.cs index 3a4760d7..0b3b73fc 100644 --- a/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubExtensions.cs +++ b/test/IbanNet.Tests/DependencyInjection/FluentAssertions/IbanNetOptionsBuilderStubExtensions.cs @@ -2,7 +2,7 @@ public static class IbanNetOptionsBuilderStubExtensions { - public static IbanNetOptionsBuilderStubAssertions Should(this Mock instance) + public static IbanNetOptionsBuilderStubAssertions Should(this T instance) where T : class, IIbanNetOptionsBuilder { return new(instance); diff --git a/test/IbanNet.Tests/DependencyInjection/IbanNetOptionsBuilderTests.cs b/test/IbanNet.Tests/DependencyInjection/IbanNetOptionsBuilderTests.cs index 15cd1728..ad56bd97 100644 --- a/test/IbanNet.Tests/DependencyInjection/IbanNetOptionsBuilderTests.cs +++ b/test/IbanNet.Tests/DependencyInjection/IbanNetOptionsBuilderTests.cs @@ -8,23 +8,21 @@ namespace IbanNet.DependencyInjection; public class IbanNetOptionsBuilderTests { - private readonly Mock _builderStub; - private readonly IIbanNetOptionsBuilder _builder; + private readonly IIbanNetOptionsBuilder _builderStub; protected IbanNetOptionsBuilderTests() { - _builder = GetBuilderStub(); - _builderStub = Mock.Get(_builder); + _builderStub = GetBuilderStub(); } private static IIbanNetOptionsBuilder GetBuilderStub() { - var builderMock = new Mock(); + IIbanNetOptionsBuilder? builderMock = Substitute.For(); builderMock - .Setup(m => m.Configure(It.IsAny>())) - .Returns(builderMock.Object); + .Configure(Arg.Any>()) + .Returns(builderMock); - return builderMock.Object; + return builderMock; } public class ExtensionTests : IbanNetOptionsBuilderTests @@ -37,7 +35,7 @@ public void Given_registry_is_configured_it_should_set_registry() .ToList(); // Act - IIbanNetOptionsBuilder returnedBuilder = _builder.UseRegistry(new IbanRegistry + IIbanNetOptionsBuilder returnedBuilder = _builderStub.UseRegistry(new IbanRegistry { Providers = { @@ -47,7 +45,7 @@ public void Given_registry_is_configured_it_should_set_registry() // Assert _builderStub.Should().HaveConfiguredRegistry(limitedCountries); - returnedBuilder.Should().BeSameAs(_builderStub.Object); + returnedBuilder.Should().BeSameAs(_builderStub); } [Fact] @@ -56,7 +54,7 @@ public void Given_registry_without_providers_is_configured_it_should_throw() var registry = new IbanRegistry(); // Act - Action act = () => _builder.UseRegistry(registry); + Action act = () => _builderStub.UseRegistry(registry); // Assert act.Should() @@ -71,11 +69,11 @@ public void Given_registry_provider_is_configured_it_should_use_provider() var customProvider = new IbanRegistryListProvider(new[] { new IbanCountry("XX") }); // Act - IIbanNetOptionsBuilder returnedBuilder = _builder.UseRegistryProvider(customProvider); + IIbanNetOptionsBuilder returnedBuilder = _builderStub.UseRegistryProvider(customProvider); // Assert _builderStub.Should().HaveConfiguredRegistry(customProvider); - returnedBuilder.Should().BeSameAs(_builderStub.Object); + returnedBuilder.Should().BeSameAs(_builderStub); } [Fact] @@ -84,11 +82,11 @@ public void Given_multiple_registry_providers_are_configured_it_should_use_provi var customProvider = new IbanRegistryListProvider(new[] { new IbanCountry("XX") }); // Act - IIbanNetOptionsBuilder returnedBuilder = _builder.UseRegistryProvider(new SwiftRegistryProvider(), customProvider); + IIbanNetOptionsBuilder returnedBuilder = _builderStub.UseRegistryProvider(new SwiftRegistryProvider(), customProvider); // Assert _builderStub.Should().HaveConfiguredRegistry(new SwiftRegistryProvider().Concat(customProvider)); - returnedBuilder.Should().BeSameAs(_builderStub.Object); + returnedBuilder.Should().BeSameAs(_builderStub); } [Fact] @@ -97,13 +95,13 @@ public void Given_rule_is_configured_via_factory_it_should_add_instance_to_rule_ var configuredRule = new TestValidationRule(); // Act - IIbanNetOptionsBuilder returnedBuilder = _builder.WithRule(() => configuredRule); + IIbanNetOptionsBuilder returnedBuilder = _builderStub.WithRule(() => configuredRule); // Assert _builderStub.Should() .HaveConfiguredRule() .And.Contain(configuredRule); - returnedBuilder.Should().BeSameAs(_builderStub.Object); + returnedBuilder.Should().BeSameAs(_builderStub); } } @@ -133,7 +131,7 @@ public void Given_null_instance_when_calling_method_it_should_throw(params objec DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.UseRegistry, instance, - Mock.Of()), + Substitute.For()), DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.UseRegistryProvider, instance, diff --git a/test/IbanNet.Tests/IbanParserTests.cs b/test/IbanNet.Tests/IbanParserTests.cs index 5fcd169f..ba54b29a 100644 --- a/test/IbanNet.Tests/IbanParserTests.cs +++ b/test/IbanNet.Tests/IbanParserTests.cs @@ -6,12 +6,12 @@ namespace IbanNet; public class IbanParserTests { - private readonly IbanValidatorStub _ibanValidatorStub; + private readonly IIbanValidator _ibanValidatorStub; private readonly IbanParser _sut; protected IbanParserTests() { - _ibanValidatorStub = new IbanValidatorStub(); + _ibanValidatorStub = IbanValidatorStub.Create(); _sut = new IbanParser(_ibanValidatorStub); } @@ -129,16 +129,15 @@ public void Given_that_iban_contains_whitespace_or_lowercase_when_parsing_it_sho { const string expectedNormalizedIban = "NL91ABNA0417164300"; _ibanValidatorStub - .Setup(m => m.Validate(expectedNormalizedIban)) - .Returns(new ValidationResult { AttemptedValue = iban, Country = new IbanCountry("NL") }) - .Verifiable(); + .Validate(Arg.Any()) + .Returns(new ValidationResult { AttemptedValue = iban, Country = new IbanCountry("NL") }); // Act Iban actual = _sut.Parse(iban); // Assert actual.ToString().Should().Be(expectedNormalizedIban); - _ibanValidatorStub.Verify(); + _ibanValidatorStub.Received(1).Validate(expectedNormalizedIban); } [Fact] @@ -187,7 +186,7 @@ public void With_value_that_causes_custom_rule_to_throw_should_rethrow() ex.Result.Should().BeNull(); ex.InnerException.Should().NotBeNull(); ex.Message.Should().Contain("is not a valid IBAN."); - _ibanValidatorStub.Verify(m => m.Validate(TestValues.IbanForCustomRuleException), Times.Once); + _ibanValidatorStub.Received(1).Validate(TestValues.IbanForCustomRuleException); } } @@ -214,7 +213,7 @@ public void With_invalid_value_should_return_false() actual.Should().BeFalse("the provided value was invalid"); iban.Should().BeNull("parsing did not succeed"); - _ibanValidatorStub.Verify(m => m.Validate(TestValues.InvalidIban), Times.Once); + _ibanValidatorStub.Received(1).Validate(TestValues.InvalidIban); } [Fact] @@ -232,7 +231,7 @@ public void With_valid_value_should_pass() .Should() .Be(TestValues.ValidIban); - _ibanValidatorStub.Verify(m => m.Validate(TestValues.ValidIban), Times.Once); + _ibanValidatorStub.Received(1).Validate(TestValues.ValidIban); } } } diff --git a/test/IbanNet.Tests/IbanTests.cs b/test/IbanNet.Tests/IbanTests.cs index adc32177..dd104869 100644 --- a/test/IbanNet.Tests/IbanTests.cs +++ b/test/IbanNet.Tests/IbanTests.cs @@ -135,7 +135,7 @@ public class When_comparing_for_equality : IbanTests public When_comparing_for_equality() { - var ibanParser = new IbanParser(new IbanValidatorStub()); + var ibanParser = new IbanParser(IbanValidatorStub.Create()); _iban = ibanParser.Parse(TestValues.ValidIban); _equalIban = ibanParser.Parse(TestValues.ValidIbanPartitioned); @@ -216,7 +216,7 @@ public class When_comparing_for_inequality : IbanTests public When_comparing_for_inequality() { - var ibanParser = new IbanParser(new IbanValidatorStub()); + var ibanParser = new IbanParser(IbanValidatorStub.Create()); _iban = ibanParser.Parse(TestValues.ValidIban); _equalIban = ibanParser.Parse(TestValues.ValidIbanPartitioned); diff --git a/test/IbanNet.Tests/IbanValidatorTests.cs b/test/IbanNet.Tests/IbanValidatorTests.cs index 924a47ab..bea3c62f 100644 --- a/test/IbanNet.Tests/IbanValidatorTests.cs +++ b/test/IbanNet.Tests/IbanValidatorTests.cs @@ -4,6 +4,7 @@ using IbanNet.Validation; using IbanNet.Validation.Results; using IbanNet.Validation.Rules; +using NSubstitute.ExceptionExtensions; namespace IbanNet; @@ -28,11 +29,11 @@ public void When_creating_instance_it_should_throw(Func act, Type yield return new object?[] { () => new IbanValidator((IbanValidatorOptions)null!), typeof(ArgumentNullException), "options" }; yield return new object?[] { () => new IbanValidator((IIbanRegistry)null!), typeof(ArgumentNullException), "registry" }; yield return new object?[] { () => new IbanValidator(null!, Array.Empty()), typeof(ArgumentNullException), "registry" }; - yield return new object?[] { () => new IbanValidator(Mock.Of(), null!), typeof(ArgumentNullException), "rules" }; + yield return new object?[] { () => new IbanValidator(Substitute.For(), null!), typeof(ArgumentNullException), "rules" }; yield return new object?[] { () => new IbanValidator(new IbanValidatorOptions { Registry = null! }), typeof(ArgumentNullException), "registry" }; yield return new object?[] { () => new IbanValidator(new IbanValidatorOptions(), null!), typeof(ArgumentNullException), "validationRuleResolver" }; - yield return new object?[] { () => new IbanValidator(null!, Mock.Of()), typeof(ArgumentNullException), "options" }; - yield return new object?[] { () => new IbanValidator(new IbanValidatorOptions { Registry = null! }, Mock.Of()), typeof(ArgumentException), "options" }; + yield return new object?[] { () => new IbanValidator(null!, Substitute.For()), typeof(ArgumentNullException), "options" }; + yield return new object?[] { () => new IbanValidator(new IbanValidatorOptions { Registry = null! }, Substitute.For()), typeof(ArgumentException), "options" }; } } @@ -91,18 +92,18 @@ public void It_should_set_property() public class Given_custom_rule_is_added : IbanValidatorTests { private readonly IbanValidator _sut; - private readonly Mock _customValidationRuleMock; + private readonly IIbanValidationRule _customValidationRuleMock; public Given_custom_rule_is_added() { - _customValidationRuleMock = new Mock(); + _customValidationRuleMock = Substitute.For(); _customValidationRuleMock - .Setup(m => m.Validate(It.IsAny())) + .Validate(Arg.Any()) .Returns(ValidationRuleResult.Success); _sut = new IbanValidator(new IbanValidatorOptions { - Rules = { _customValidationRuleMock.Object } + Rules = { _customValidationRuleMock } }); } @@ -115,7 +116,7 @@ public void When_validating_should_call_custom_rule() _sut.Validate(iban); // Assert - _customValidationRuleMock.Verify(m => m.Validate(It.Is(ctx => ctx.Value == iban)), Times.Once); + _customValidationRuleMock.Received(1).Validate(Arg.Is(ctx => ctx.Value == iban)); } [Fact] @@ -125,7 +126,7 @@ public void Given_custom_rule_throws_when_validating_should_wrap_as_exceptionRes Exception exception = new InvalidOperationException("My custom error"); _customValidationRuleMock - .Setup(m => m.Validate(It.IsAny())) + .Validate(Arg.Any()) .Throws(exception); // Act @@ -147,7 +148,7 @@ public void Given_custom_rule_fails_when_validating_should_not_validate() const string errorMessage = "My custom error"; _customValidationRuleMock - .Setup(m => m.Validate(It.IsAny())) + .Validate(Arg.Any()) .Returns(new ErrorResult(errorMessage)); // Act diff --git a/test/IbanNet.Tests/Json/IbanJsonConverterTests.cs b/test/IbanNet.Tests/Json/IbanJsonConverterTests.cs index 32601371..82cc1672 100644 --- a/test/IbanNet.Tests/Json/IbanJsonConverterTests.cs +++ b/test/IbanNet.Tests/Json/IbanJsonConverterTests.cs @@ -13,7 +13,7 @@ public class IbanJsonConverterTests public void When_creating_default_instance_it_should_use_static_validator() { const string ibanStr = "NL91ABNA0417164300"; - var validatorStub = new IbanValidatorStub(); + IIbanValidator validatorStub = IbanValidatorStub.Create(); ReadOnlySpan buffer = Encoding.UTF8.GetBytes($"\"{ibanStr}\""); var reader = new Utf8JsonReader(buffer); @@ -28,7 +28,7 @@ public void When_creating_default_instance_it_should_use_static_validator() sut.Read(ref reader, typeof(Iban), new JsonSerializerOptions()); // Assert - validatorStub.Verify(m => m.Validate(ibanStr), Times.Once); + validatorStub.Received(1).Validate(ibanStr); } finally { @@ -41,24 +41,22 @@ public void Given_that_parser_is_used_when_creating_instance_it_should_use_parse { const string ibanStr = "NL91ABNA0417164300"; - Iban? outIban = null; - var parserMock = new Mock(); + IIbanParser parserMock = Substitute.For(); parserMock - .Setup(m => m.TryParse(It.IsAny(), out outIban)) - .Returns(true) - .Verifiable(); + .TryParse(Arg.Any(), out Arg.Any()) + .Returns(true); ReadOnlySpan buffer = Encoding.UTF8.GetBytes($"\"{ibanStr}\""); var reader = new Utf8JsonReader(buffer); reader.Read(); - var sut = new IbanJsonConverter(parserMock.Object); + var sut = new IbanJsonConverter(parserMock); // Act sut.Read(ref reader, typeof(Iban), new JsonSerializerOptions()); // Assert - parserMock.Verify(); + parserMock.Received(1).TryParse(ibanStr, out Arg.Any()); } [Fact] @@ -97,7 +95,7 @@ public void Given_that_a_complex_record_has_a_null_iban_when_deserializing_it_sh [MemberData(nameof(WriterNullArgTestCases))] public void Given_that_write_argument_is_null_when_writing_it_should_throw(Utf8JsonWriter writer, Iban value, string expectedParamName) { - var sut = new IbanJsonConverter(Mock.Of()); + var sut = new IbanJsonConverter(Substitute.For()); // Act Action act = () => sut.Write(writer, value, new JsonSerializerOptions()); diff --git a/test/IbanNet.Tests/Registry/IbanRegistryTests.cs b/test/IbanNet.Tests/Registry/IbanRegistryTests.cs index 283afd36..2670ec5e 100644 --- a/test/IbanNet.Tests/Registry/IbanRegistryTests.cs +++ b/test/IbanNet.Tests/Registry/IbanRegistryTests.cs @@ -120,7 +120,7 @@ public void Given_that_registry_is_not_yet_hydrated_when_adding_provider_it_shou }; // Act - Action act = () => sut.Providers.Add(Mock.Of()); + Action act = () => sut.Providers.Add(Substitute.For()); // Assert act.Should().NotThrow(); @@ -140,7 +140,7 @@ public void Given_that_registry_has_been_hydrated_when_adding_another_provider_i // Act sut.Count.Should().BeGreaterThan(0); // Hydrate - Action act = () => sut.Providers.Add(Mock.Of()); + Action act = () => sut.Providers.Add(Substitute.For()); // Assert act.Should().Throw() @@ -162,7 +162,7 @@ public void Given_that_registry_has_been_hydrated_when_adding_another_provider_t // Act IList providerRef = sut.Providers; sut.Count.Should().BeGreaterThan(0); // Hydrate - providerRef.Add(Mock.Of()); + providerRef.Add(Substitute.For()); // Assert sut.Providers.Should().HaveCount(1); diff --git a/test/IbanNet.Tests/Registry/Patterns/PatternTests.cs b/test/IbanNet.Tests/Registry/Patterns/PatternTests.cs index 65e8a502..0d59064e 100644 --- a/test/IbanNet.Tests/Registry/Patterns/PatternTests.cs +++ b/test/IbanNet.Tests/Registry/Patterns/PatternTests.cs @@ -2,11 +2,11 @@ public class PatternTests { - private readonly Mock _tokenizerMock; + private readonly FakePatternTokenizer _tokenizerMock; protected PatternTests() { - _tokenizerMock = new Mock(); + _tokenizerMock = Substitute.For(); } public class Given_a_pattern_created_with_tokens : PatternTests @@ -46,7 +46,7 @@ public void When_getting_tokens_it_should_return_expected() sut.Tokens.Should() .BeAssignableTo>() .And.BeEquivalentTo(_tokens); - _tokenizerMock.Verify(m => m.Tokenize(It.IsAny>()), Times.Never); + _tokenizerMock.DidNotReceiveWithAnyArgs().Tokenize(default(IEnumerable)!); } [Fact] @@ -85,9 +85,8 @@ public Given_a_pattern_created_with_string_pattern_and_tokenizer() }; _tokenizerMock - .Setup(m => m.Tokenize((IEnumerable)TestPattern)) - .Returns(_tokens) - .Verifiable(); + .Tokenize(Arg.Any>()) + .Returns(x => x.Arg>().SequenceEqual(TestPattern) ? _tokens : Enumerable.Empty()); } [Fact] @@ -96,7 +95,7 @@ public void When_creating_with_null_pattern_it_should_throw() string? pattern = null; // Act - Func act = () => new FakePattern(pattern!, Mock.Of>()); + Func act = () => new FakePattern(pattern!, Substitute.For>()); // Assert act.Should() @@ -122,32 +121,32 @@ public void When_creating_with_null_tokenizer_it_should_throw() public void When_getting_tokens_it_should_return_expected() { // Act - var sut = new FakePattern(TestPattern, _tokenizerMock.Object); + var sut = new FakePattern(TestPattern, _tokenizerMock); // Assert sut.Tokens.Should() .BeAssignableTo>() .And.BeEquivalentTo(_tokens); - _tokenizerMock.Verify(); + _tokenizerMock.Received(1).Tokenize(Arg.Is>(x => x.SequenceEqual(TestPattern))); } [Fact] public void When_getting_tokens_multiple_times_it_should_only_tokenize_once() { // Act - var sut = new FakePattern(TestPattern, _tokenizerMock.Object); + var sut = new FakePattern(TestPattern, _tokenizerMock); // Assert sut.Tokens.Should().BeEquivalentTo(_tokens); sut.Tokens.Should().HaveCount(2); - _tokenizerMock.Verify(m => m.Tokenize(It.IsAny>()), Times.Once); + _tokenizerMock.Received(1).Tokenize(Arg.Is>(x => x.SequenceEqual(TestPattern))); } [Fact] public void When_getting_string_representation_it_should_return_expected() { // Act - var sut = new FakePattern(TestPattern, _tokenizerMock.Object); + var sut = new FakePattern(TestPattern, _tokenizerMock); // Assert sut.ToString().Should().Be(TestPattern); @@ -161,7 +160,7 @@ public void Given_that_at_least_one_token_is_not_fixed_length_when_getting_patte { _tokens.AddRange(Enumerable.Repeat(new PatternToken(AsciiCategory.UppercaseLetter, 1, 3), nonFixedTokensToAdd)); - new FakePattern(TestPattern, _tokenizerMock.Object).IsFixedLength.Should().Be(shouldBeFixedLength); + new FakePattern(TestPattern, _tokenizerMock).IsFixedLength.Should().Be(shouldBeFixedLength); } } diff --git a/test/IbanNet.Tests/TypeConverters/IbanTypeConverterTests.cs b/test/IbanNet.Tests/TypeConverters/IbanTypeConverterTests.cs index 27e92122..47d63355 100644 --- a/test/IbanNet.Tests/TypeConverters/IbanTypeConverterTests.cs +++ b/test/IbanNet.Tests/TypeConverters/IbanTypeConverterTests.cs @@ -64,19 +64,19 @@ public void From_null_iban_string_should_return_null() [Fact] public void Given_that_parser_can_be_resolved_when_converting_from_string_it_should_not_resolve_validator() { - var typeDescriptorContextMock = new Mock(); + var typeDescriptorContextMock = Substitute.For(); typeDescriptorContextMock - .Setup(m => m.GetService(It.Is(t => t == typeof(IIbanParser)))) - .Returns(new IbanParser(IbanRegistry.Default)) - .Verifiable(); + .GetService(Arg.Is(t => t == typeof(IIbanParser))) + .Returns(new IbanParser(IbanRegistry.Default)); // Act - object? resultObj = _sut.ConvertFrom(typeDescriptorContextMock.Object, CultureInfo.InvariantCulture, TestValues.ValidIban); + object? resultObj = _sut.ConvertFrom(typeDescriptorContextMock, CultureInfo.InvariantCulture, TestValues.ValidIban); // Assert - typeDescriptorContextMock.Verify(); + typeDescriptorContextMock.Received(1).GetService(typeof(IIbanParser)); typeDescriptorContextMock - .Verify(m => m.GetService(It.Is(t => t == typeof(IIbanValidator))), Times.Never); + .DidNotReceive() + .GetService(Arg.Is(t => t == typeof(IIbanValidator))); resultObj.Should() .NotBeNull() .And.BeOfType() @@ -88,21 +88,20 @@ public void Given_that_parser_can_be_resolved_when_converting_from_string_it_sho [Fact] public void Given_that_parser_cannot_be_resolved_when_converting_from_string_it_should_request_validator_next() { - var typeDescriptorContextMock = new Mock(); + var typeDescriptorContextMock = Substitute.For(); typeDescriptorContextMock - .Setup(m => m.GetService(It.Is(t => t == typeof(IIbanParser)))) - .Returns(null) - .Verifiable(); + .GetService(Arg.Is(t => t == typeof(IIbanParser))) + .Returns(null); typeDescriptorContextMock - .Setup(m => m.GetService(It.Is(t => t == typeof(IIbanValidator)))) - .Returns(new IbanValidator()) - .Verifiable(); + .GetService(Arg.Is(t => t == typeof(IIbanValidator))) + .Returns(new IbanValidator()); // Act - object? resultObj = _sut.ConvertFrom(typeDescriptorContextMock.Object, CultureInfo.InvariantCulture, TestValues.ValidIban); + object? resultObj = _sut.ConvertFrom(typeDescriptorContextMock, CultureInfo.InvariantCulture, TestValues.ValidIban); // Assert - typeDescriptorContextMock.Verify(); + typeDescriptorContextMock.Received(1).GetService(typeof(IIbanParser)); + typeDescriptorContextMock.Received(1).GetService(typeof(IIbanValidator)); resultObj.Should() .NotBeNull() .And.BeOfType() diff --git a/test/IbanNet.Tests/Validation/DefaultValidationRuleResolverTests.cs b/test/IbanNet.Tests/Validation/DefaultValidationRuleResolverTests.cs index 1f58754c..15853a46 100644 --- a/test/IbanNet.Tests/Validation/DefaultValidationRuleResolverTests.cs +++ b/test/IbanNet.Tests/Validation/DefaultValidationRuleResolverTests.cs @@ -10,10 +10,10 @@ public class DefaultValidationRuleResolverTests public DefaultValidationRuleResolverTests() { - var registryMock = new Mock(); - registryMock.Setup(m => m.Providers).Returns(new List()); + IIbanRegistry registryMock = Substitute.For(); + registryMock.Providers.Returns(new List()); _customRules = new List(); - _sut = new DefaultValidationRuleResolver(registryMock.Object, _customRules); + _sut = new DefaultValidationRuleResolver(registryMock, _customRules); } [Fact] @@ -44,8 +44,8 @@ public void Given_strict_method_when_getting_rules_it_should_return_expected_rul [Fact] public void Given_custom_rules_when_getting_rules_it_should_append_custom_rules() { - IIbanValidationRule rule1 = Mock.Of(); - IIbanValidationRule rule2 = Mock.Of(); + IIbanValidationRule rule1 = Substitute.For(); + IIbanValidationRule rule2 = Substitute.For(); _customRules.Add(rule1); _customRules.Add(rule2); diff --git a/test/IbanNet.Tests/Validation/Rules/Mod97RuleTests.cs b/test/IbanNet.Tests/Validation/Rules/Mod97RuleTests.cs index 044e0204..3c9b9de2 100644 --- a/test/IbanNet.Tests/Validation/Rules/Mod97RuleTests.cs +++ b/test/IbanNet.Tests/Validation/Rules/Mod97RuleTests.cs @@ -6,12 +6,12 @@ namespace IbanNet.Validation.Rules; public class Mod97RuleTests { private readonly Mod97Rule _sut; - private readonly Mock _calculatorMock; + private readonly ICheckDigitsCalculator _calculatorMock; public Mod97RuleTests() { - _calculatorMock = new Mock(); - _sut = new Mod97Rule(_calculatorMock.Object); + _calculatorMock = Substitute.For(); + _sut = new Mod97Rule(_calculatorMock); } [Fact] @@ -20,16 +20,15 @@ public void Given_invalid_value_when_validating_it_should_return_error() const string value = "ABCD123456"; const int invalidCheckDigit = 123; _calculatorMock - .Setup(m => m.Compute(It.Is(buf => buf.SequenceEqual("123456ABCD")))) - .Returns(invalidCheckDigit) - .Verifiable(); + .Compute(Arg.Is(buf => buf.SequenceEqual("123456ABCD"))) + .Returns(invalidCheckDigit); // Act ValidationRuleResult actual = _sut.Validate(new ValidationRuleContext(value)); // Assert actual.Should().BeOfType(); - _calculatorMock.Verify(); + _calculatorMock.ReceivedWithAnyArgs(1).Compute(default!); } [Fact] @@ -38,15 +37,14 @@ public void Given_valid_value_when_validating_it_should_return_success() const string value = "ABCD123456"; const int expectedCheckDigit = 1; _calculatorMock - .Setup(m => m.Compute(It.Is(buf => buf.SequenceEqual("123456ABCD")))) - .Returns(expectedCheckDigit) - .Verifiable(); + .Compute(Arg.Is(buf => buf.SequenceEqual("123456ABCD"))) + .Returns(expectedCheckDigit); // Act ValidationRuleResult actual = _sut.Validate(new ValidationRuleContext(value)); // Assert actual.Should().Be(ValidationRuleResult.Success); - _calculatorMock.Verify(); + _calculatorMock.ReceivedWithAnyArgs(1).Compute(default!); } -} \ No newline at end of file +} diff --git a/test/TestHelpers/FluentAssertions/DelegateAssertions.cs b/test/TestHelpers/FluentAssertions/DelegateAssertions.cs index 65931a9a..ac90571b 100644 --- a/test/TestHelpers/FluentAssertions/DelegateAssertions.cs +++ b/test/TestHelpers/FluentAssertions/DelegateAssertions.cs @@ -23,7 +23,7 @@ protected override void InvokeSubject() /// /// The arguments. /// - /// A formatted phrase as is supported by explaining why the assertion + /// A formatted phrase as is supported by explaining why the assertion /// is needed. If the phrase does not start with the word because, it is prepended automatically. /// /// @@ -53,7 +53,7 @@ params object[] becauseArgs /// Asserts that the current does not throw any exception. /// /// - /// A formatted phrase as is supported by explaining why the assertion + /// A formatted phrase as is supported by explaining why the assertion /// is needed. If the phrase does not start with the word because, it is prepended automatically. /// /// @@ -90,7 +90,7 @@ params object[] becauseArgs /// /// The arguments. /// - /// A formatted phrase as is supported by explaining why the assertion + /// A formatted phrase as is supported by explaining why the assertion /// is needed. If the phrase does not start with the word because, it is prepended automatically. /// /// diff --git a/test/TestHelpers/IbanValidatorStub.cs b/test/TestHelpers/IbanValidatorStub.cs index 203491f4..04dfa0e8 100644 --- a/test/TestHelpers/IbanValidatorStub.cs +++ b/test/TestHelpers/IbanValidatorStub.cs @@ -1,35 +1,29 @@ using IbanNet; using IbanNet.Registry; using IbanNet.Validation.Results; +using NSubstitute.ExceptionExtensions; namespace TestHelpers; -public class IbanValidatorStub : Mock, IIbanValidator +public static class IbanValidatorStub { - public IbanValidatorStub() + public static IIbanValidator Create() { - Setup(m => m.Validate(It.IsAny())) - .Returns(iban => new ValidationResult + IIbanValidator mock = Substitute.For(); + mock.Validate(Arg.Any()) + .Returns(info => { - AttemptedValue = iban, - Country = IbanRegistry.Default[iban.Substring(0, 2)] + string? iban = info.Arg(); + return iban switch + { + null => new ValidationResult { AttemptedValue = null, Error = new InvalidLengthResult() }, + TestValues.InvalidIban => new ValidationResult { AttemptedValue = info.Arg(), Error = new IllegalCharactersResult(0) }, + TestValues.IbanForCustomRuleFailure => new ValidationResult { AttemptedValue = info.Arg(), Error = new ErrorResult("Custom message") }, + TestValues.IbanForCustomRuleException => throw new InvalidOperationException("Custom message"), + _ => new ValidationResult { AttemptedValue = iban, Country = IbanRegistry.Default[iban.Substring(0, 2)] } + }; }); - Setup(m => m.Validate(null)) - .Returns(new ValidationResult { AttemptedValue = null, Error = new InvalidLengthResult() }); - - Setup(m => m.Validate(TestValues.InvalidIban)) - .Returns(iban => new ValidationResult { AttemptedValue = iban, Error = new IllegalCharactersResult(0) }); - - Setup(m => m.Validate(TestValues.IbanForCustomRuleFailure)) - .Returns(iban => new ValidationResult { AttemptedValue = iban, Error = new ErrorResult("Custom message") }); - - Setup(m => m.Validate(TestValues.IbanForCustomRuleException)) - .Throws(new InvalidOperationException("Custom message")); - } - - public ValidationResult Validate(string? iban) - { - return Object.Validate(iban); + return mock; } } diff --git a/test/TestHelpers/Specs/ConfiguredMultipleRulesSpec.cs b/test/TestHelpers/Specs/ConfiguredMultipleRulesSpec.cs index 84cd3365..69faddb9 100644 --- a/test/TestHelpers/Specs/ConfiguredMultipleRulesSpec.cs +++ b/test/TestHelpers/Specs/ConfiguredMultipleRulesSpec.cs @@ -17,7 +17,7 @@ protected ConfiguredMultipleRulesSpec(IDependencyInjectionFixture fixture) : bas protected override void Given() { - IIbanValidationRule fakeRule = Mock.Of(); + IIbanValidationRule fakeRule = Substitute.For(); _fakeRuleType = fakeRule.GetType(); Fixture.Configure(builder => { diff --git a/test/TestHelpers/Specs/ShouldPreserveStaticValidatorSpec.cs b/test/TestHelpers/Specs/ShouldPreserveStaticValidatorSpec.cs index f6b46885..a35d0e89 100644 --- a/test/TestHelpers/Specs/ShouldPreserveStaticValidatorSpec.cs +++ b/test/TestHelpers/Specs/ShouldPreserveStaticValidatorSpec.cs @@ -13,7 +13,7 @@ protected ShouldPreserveStaticValidatorSpec(IDependencyInjectionFixture fixture) protected override void Given() { - Iban.Validator = _initialValidator = Mock.Of(); + Iban.Validator = _initialValidator = Substitute.For(); Fixture.Configure(_ => { }); } diff --git a/test/TestHelpers/Specs/ShouldResolveCustomService.cs b/test/TestHelpers/Specs/ShouldResolveCustomService.cs index 67c1455b..24c5cbb7 100644 --- a/test/TestHelpers/Specs/ShouldResolveCustomService.cs +++ b/test/TestHelpers/Specs/ShouldResolveCustomService.cs @@ -18,23 +18,23 @@ protected override void Given() Fixture.Configure(_ => { }); } - [Theory] - [InlineData(typeof(IIbanRegistry))] - [InlineData(typeof(IIbanParser))] - [InlineData(typeof(IIbanValidator))] - [InlineData(typeof(IIbanGenerator))] - public void When_resolving_registry_it_should_not_throw(Type serviceType) - { - Type expectedMockType = typeof(IMocked<>).MakeGenericType(serviceType); + //[Theory] + //[InlineData(typeof(IIbanRegistry))] + //[InlineData(typeof(IIbanParser))] + //[InlineData(typeof(IIbanValidator))] + //[InlineData(typeof(IIbanGenerator))] + //public void When_resolving_registry_it_should_not_throw(Type serviceType) + //{ + // Type expectedMockType = typeof(IMocked<>).MakeGenericType(serviceType); - // Assert - Func act = () => Subject.GetService(serviceType); + // // Assert + // Func act = () => Subject.GetService(serviceType); - // Act - act.Should() - .NotThrow() - .Which - .Should() - .BeAssignableTo(expectedMockType); - } + // // Act + // act.Should() + // .NotThrow() + // .Which + // .Should() + // .BeAssignableTo(expectedMockType); + //} } diff --git a/test/TestHelpers/Specs/ShouldSetStaticValidatorSpec.cs b/test/TestHelpers/Specs/ShouldSetStaticValidatorSpec.cs index 09a0bc52..8689a618 100644 --- a/test/TestHelpers/Specs/ShouldSetStaticValidatorSpec.cs +++ b/test/TestHelpers/Specs/ShouldSetStaticValidatorSpec.cs @@ -13,7 +13,7 @@ protected ShouldSetStaticValidatorSpec(IDependencyInjectionFixture fixture) : ba protected override void Given() { - Iban.Validator = _initialValidator = Mock.Of(); + Iban.Validator = _initialValidator = Substitute.For(); Fixture.Configure(_ => { }); } diff --git a/test/TestHelpers/TestValues.cs b/test/TestHelpers/TestValues.cs index 4b132ac1..0de99831 100644 --- a/test/TestHelpers/TestValues.cs +++ b/test/TestHelpers/TestValues.cs @@ -6,6 +6,6 @@ public static class TestValues public const string ValidIbanPartitioned = "AD12 0001 2030 2003 5910 0100"; public const string ValidIbanPartitionedAndWithLowercase = "ad12 0001 2030 2003 5910 0100"; public const string InvalidIban = "__INVALID_IBAN"; - public static readonly string IbanForCustomRuleFailure = "CustomRuleFailure".ToUpperInvariant(); - public static readonly string IbanForCustomRuleException = "CustomRuleException".ToUpperInvariant(); -} \ No newline at end of file + public const string IbanForCustomRuleFailure = "CUSTOMRULEFAILURE"; + public const string IbanForCustomRuleException = "CUSTOMRULEEXCEPTION"; +}