Skip to content

Commit

Permalink
#4 Add test for not invoking shim if the method is overriden in a der…
Browse files Browse the repository at this point in the history
…ived class
  • Loading branch information
sguldmund committed Jan 13, 2024
1 parent 617bc07 commit 7cb5636
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion test/Pose.Tests/ShimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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, string>(
(AbstractBase @this) =>
{
wasCalled = true;
return "Hello";
});
var shim = Shim
.Replace(() => Is.A<AbstractBase>().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");
}
}
}

0 comments on commit 7cb5636

Please sign in to comment.