From 7cb5636e9257e3539f9518a3a5de4510b2a3625c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Guldmund?= Date: Sat, 13 Jan 2024 14:25:51 +0100 Subject: [PATCH] #4 Add test for not invoking shim if the method is overriden in a derived class --- test/Pose.Tests/ShimTests.cs | 40 +++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/test/Pose.Tests/ShimTests.cs b/test/Pose.Tests/ShimTests.cs index 1b8c849..12d612d 100644 --- a/test/Pose.Tests/ShimTests.cs +++ b/test/Pose.Tests/ShimTests.cs @@ -424,10 +424,15 @@ public void Can_invoke_constructor_in_isolation() private abstract class AbstractBase { - public string GetStringFromAbstractBase() => "!"; + public virtual string GetStringFromAbstractBase() => "!"; } private class DerivedFromAbstractBase : AbstractBase { } + + private class ShadowsMethodFromAbstractBase : AbstractBase + { + public override string GetStringFromAbstractBase() => "Shadow"; + } [Fact] public void Can_shim_instance_method_of_abstract_type() @@ -452,5 +457,38 @@ public void Can_shim_instance_method_of_abstract_type() // Assert dt.Should().BeEquivalentTo("Hello", because: "the shim configured the base class"); } + + [Fact] + public void Shim_is_not_invoked_if_method_is_overriden_in_derived_type() + { + // Arrange + var wasCalled = false; + var action = new Func( + (AbstractBase @this) => + { + wasCalled = true; + return "Hello"; + }); + var shim = Shim + .Replace(() => Is.A().GetStringFromAbstractBase()) + .With(action); + + // Act + string dt = default; + wasCalled.Should().BeFalse(because: "no calls have been made yet"); + PoseContext.Isolate( + () => + { + var instance = new ShadowsMethodFromAbstractBase(); + dt = instance.GetStringFromAbstractBase(); + }, + shim + ); + + // Assert + var _ = new ShadowsMethodFromAbstractBase(); + dt.Should().BeEquivalentTo(_.GetStringFromAbstractBase(), because: "the shim configured the base class"); + wasCalled.Should().BeFalse(because: "the shim was not invoked"); + } } }