From 598270d3f19702cab84c0f4ac4b1d63459e8e0fc Mon Sep 17 00:00:00 2001 From: Benjamin Evenson <2031163+benjiro@users.noreply.github.com> Date: Tue, 22 Aug 2023 01:45:48 +1000 Subject: [PATCH] test: Replace remaining Moq with NSubstitute (#143) Signed-off-by: Benjamin Evenson <2031163+benjiro@users.noreply.github.com> --- .../OpenFeature.Tests.csproj | 1 - .../OpenFeature.Tests/OpenFeatureHookTests.cs | 97 ++++++++----------- 2 files changed, 43 insertions(+), 55 deletions(-) diff --git a/test/OpenFeature.Tests/OpenFeature.Tests.csproj b/test/OpenFeature.Tests/OpenFeature.Tests.csproj index 6c719638..d1b77419 100644 --- a/test/OpenFeature.Tests/OpenFeature.Tests.csproj +++ b/test/OpenFeature.Tests/OpenFeature.Tests.csproj @@ -18,7 +18,6 @@ - diff --git a/test/OpenFeature.Tests/OpenFeatureHookTests.cs b/test/OpenFeature.Tests/OpenFeatureHookTests.cs index aa0c75c4..c814b15a 100644 --- a/test/OpenFeature.Tests/OpenFeatureHookTests.cs +++ b/test/OpenFeature.Tests/OpenFeatureHookTests.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using AutoFixture; using FluentAssertions; -using Moq; using NSubstitute; using NSubstitute.ExceptionExtensions; using OpenFeature.Constant; @@ -436,45 +435,40 @@ public async Task Error_Occurs_During_Before_After_Evaluation_Should_Not_Invoke_ [Specification("4.5.1", "`Flag evaluation options` MAY contain `hook hints`, a map of data to be provided to hook invocations.")] public async Task Hook_Hints_May_Be_Optional() { - var featureProvider = new Mock(MockBehavior.Strict); - var hook = new Mock(MockBehavior.Strict); - var defaultEmptyHookHints = new Dictionary(); - var flagOptions = new FlagEvaluationOptions(hook.Object); - EvaluationContext evaluationContext = null; - - var sequence = new MockSequence(); + var featureProvider = Substitute.For(); + var hook = Substitute.For(); + var flagOptions = new FlagEvaluationOptions(hook); - featureProvider.Setup(x => x.GetMetadata()) + featureProvider.GetMetadata() .Returns(new Metadata(null)); - featureProvider.Setup(x => x.GetProviderHooks()) + featureProvider.GetProviderHooks() .Returns(ImmutableList.Empty); - hook.InSequence(sequence) - .Setup(x => x.Before(It.IsAny>(), defaultEmptyHookHints)) - .ReturnsAsync(evaluationContext); + hook.Before(Arg.Any>(), Arg.Any>()) + .Returns(EvaluationContext.Empty); - featureProvider.InSequence(sequence) - .Setup(x => x.ResolveBooleanValue(It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(new ResolutionDetails("test", false)); + featureProvider.ResolveBooleanValue("test", false, Arg.Any()) + .Returns(new ResolutionDetails("test", false)); - hook.InSequence(sequence) - .Setup(x => x.After(It.IsAny>(), It.IsAny>(), defaultEmptyHookHints)) - .Returns(Task.CompletedTask); + hook.After(Arg.Any>(), Arg.Any>(), Arg.Any>()) + .Returns(Task.FromResult(Task.CompletedTask)); - hook.InSequence(sequence) - .Setup(x => x.Finally(It.IsAny>(), defaultEmptyHookHints)) + hook.Finally(Arg.Any>(), Arg.Any>()) .Returns(Task.CompletedTask); - Api.Instance.SetProvider(featureProvider.Object); + Api.Instance.SetProvider(featureProvider); var client = Api.Instance.GetClient(); - await client.GetBooleanValue("test", false, config: flagOptions); + await client.GetBooleanValue("test", false, EvaluationContext.Empty, flagOptions); - hook.Verify(x => x.Before(It.IsAny>(), defaultEmptyHookHints), Times.Once); - hook.Verify(x => x.After(It.IsAny>(), It.IsAny>(), defaultEmptyHookHints), Times.Once); - hook.Verify(x => x.Finally(It.IsAny>(), defaultEmptyHookHints), Times.Once); - featureProvider.Verify(x => x.ResolveBooleanValue(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + Received.InOrder(() => + { + hook.Received().Before(Arg.Any>(), Arg.Any>()); + featureProvider.Received().ResolveBooleanValue("test", false, Arg.Any()); + hook.Received().After(Arg.Any>(), Arg.Any>(), Arg.Any>()); + hook.Received().Finally(Arg.Any>(), Arg.Any>()); + }); } [Fact] @@ -515,52 +509,47 @@ public async Task When_Error_Occurs_In_Before_Hook_Should_Return_Default_Value() [Specification("4.4.5", "If an error occurs in the `before` or `after` hooks, the `error` hooks MUST be invoked.")] public async Task When_Error_Occurs_In_After_Hook_Should_Invoke_Error_Hook() { - var featureProvider = new Mock(MockBehavior.Strict); - var hook = new Mock(MockBehavior.Strict); - var defaultEmptyHookHints = new Dictionary(); - var flagOptions = new FlagEvaluationOptions(hook.Object); + var featureProvider = Substitute.For(); + var hook = Substitute.For(); + var flagOptions = new FlagEvaluationOptions(hook); var exceptionToThrow = new Exception("Fails during default"); - EvaluationContext evaluationContext = null; - - var sequence = new MockSequence(); - featureProvider.Setup(x => x.GetMetadata()) + featureProvider.GetMetadata() .Returns(new Metadata(null)); - featureProvider.Setup(x => x.GetProviderHooks()) + featureProvider.GetProviderHooks() .Returns(ImmutableList.Empty); - hook.InSequence(sequence) - .Setup(x => x.Before(It.IsAny>(), defaultEmptyHookHints)) - .ReturnsAsync(evaluationContext); + hook.Before(Arg.Any>(), Arg.Any>()) + .Returns(EvaluationContext.Empty); - featureProvider.InSequence(sequence) - .Setup(x => x.ResolveBooleanValue(It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(new ResolutionDetails("test", false)); + featureProvider.ResolveBooleanValue(Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(new ResolutionDetails("test", false)); - hook.InSequence(sequence) - .Setup(x => x.After(It.IsAny>(), It.IsAny>(), defaultEmptyHookHints)) + hook.After(Arg.Any>(), Arg.Any>(), Arg.Any>()) .ThrowsAsync(exceptionToThrow); - hook.InSequence(sequence) - .Setup(x => x.Error(It.IsAny>(), It.IsAny(), defaultEmptyHookHints)) + hook.Error(Arg.Any>(), Arg.Any(), Arg.Any>()) .Returns(Task.CompletedTask); - hook.InSequence(sequence) - .Setup(x => x.Finally(It.IsAny>(), defaultEmptyHookHints)) + hook.Finally(Arg.Any>(), Arg.Any>()) .Returns(Task.CompletedTask); - Api.Instance.SetProvider(featureProvider.Object); + Api.Instance.SetProvider(featureProvider); var client = Api.Instance.GetClient(); var resolvedFlag = await client.GetBooleanValue("test", true, config: flagOptions); resolvedFlag.Should().BeTrue(); - hook.Verify(x => x.Before(It.IsAny>(), defaultEmptyHookHints), Times.Once); - hook.Verify(x => x.After(It.IsAny>(), It.IsAny>(), defaultEmptyHookHints), Times.Once); - hook.Verify(x => x.Error(It.IsAny>(), exceptionToThrow, defaultEmptyHookHints), Times.Once); - hook.Verify(x => x.Finally(It.IsAny>(), defaultEmptyHookHints), Times.Once); - featureProvider.Verify(x => x.ResolveBooleanValue(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + + Received.InOrder(() => + { + hook.Received(1).Before(Arg.Any>(), Arg.Any>()); + hook.Received(1).After(Arg.Any>(), Arg.Any>(), Arg.Any>()); + hook.Received(1).Finally(Arg.Any>(), Arg.Any>()); + }); + + await featureProvider.DidNotReceive().ResolveBooleanValue("test", false, Arg.Any()); } } }