Skip to content

Commit

Permalink
#4 Add more tests for abstract methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sguldmund committed Jan 13, 2024
1 parent 7da4ebb commit e098e61
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion test/Pose.Tests/ShimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,20 @@ public void Can_invoke_constructor_in_isolation()
private abstract class AbstractBase
{
public virtual string GetStringFromAbstractBase() => "!";

public abstract string GetAbstractString();
}

private class DerivedFromAbstractBase : AbstractBase { }
private class DerivedFromAbstractBase : AbstractBase
{
public override string GetAbstractString() => throw new NotImplementedException();
}

private class ShadowsMethodFromAbstractBase : AbstractBase
{
public override string GetStringFromAbstractBase() => "Shadow";

public override string GetAbstractString() => throw new NotImplementedException();
}

[Fact]
Expand All @@ -458,6 +465,42 @@ public void Can_shim_instance_method_of_abstract_type()
dt.Should().BeEquivalentTo("Hello", because: "the shim configured the base class");
}

[Fact]
public void Can_shim_abstract_method_of_abstract_type()
{
// Arrange
const string returnValue = "Hello";

var wasCalled = false;
var action = new Func<AbstractBase, string>(
(AbstractBase @this) =>
{
wasCalled = true;
return returnValue;
});
var shim = Shim
.Replace(() => Is.A<AbstractBase>().GetAbstractString())
.With(action);

// Act
string dt = default;
wasCalled.Should().BeFalse(because: "no calls have been made yet");
// ReSharper disable once SuggestVarOrType_SimpleTypes
Action act = () => PoseContext.Isolate(
() =>
{
var instance = new DerivedFromAbstractBase();
dt = instance.GetAbstractString();
},
shim
);

// Assert
act.Should().NotThrow(because: "the shim works");
wasCalled.Should().BeTrue(because: "the shim has been invoked");
dt.Should().BeEquivalentTo(returnValue, because: "the shim configured the base class");
}

[Fact]
public void Shim_is_not_invoked_if_method_is_overriden_in_derived_type()
{
Expand Down

0 comments on commit e098e61

Please sign in to comment.