Miista / pose

Replace any .NET method (including static and non-virtual) with a delegate
MIT License
24 stars 3 forks source link

Document extension method shimming #27

Open Miista opened 8 months ago

Miista commented 8 months ago

Please refer to tonerdo/pose#22

I will add tests to ensure that we continue to support this.

Tests

Defined using Called using Expected result
Static method invocation Static method invocation :heavy_check_mark:
Static method invocation Extension method invocation :heavy_check_mark:
Extension method invocation Static method invocation :heavy_check_mark:
Extension method invocation Extension method invocation :heavy_check_mark:
Miista commented 8 months ago

I can confirm that we do support shimming extension methods. Please see the following MVE:

public static class IntExtensions
{
    // The extension method defaults to stringifying the int
    public static string GetFunnyName(this int i) => i.ToString();
}

public class Program
{
    public static void Main(string[] args)
    {
        var extensionShim = Shim
            .Replace(() => IntExtensions.GetFunnyName(Is.A<int>()))
            // We overwrite the extension method to simply return "H"
            .With(delegate(int i) { return "H"; });

        PoseContext.Isolate(()=>
        {
            var i = 0;
            Console.WriteLine(i.GetFunnyName());
        }, extensionShim);
    }
}

Please note that the shim can be defined in one of the following ways.

Static method invocation

Shim
    .Replace(() => IntExtensions.GetFunnyName(Is.A<int>()))
    .With(delegate(int i) { return "H"; });

Extension method invocation

Shim
    .Replace(() => Is.A<int>().GetFunnyName())
    .With(delegate(int i) { return "H"; });