tonerdo / pose

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

Issue with .Net static methods like File.Exists #70

Open sberthu opened 2 years ago

sberthu commented 2 years ago

Hello, In .Net5 Framework, I have a Xunit Test class like this:

using Pose;
using System.IO;
using Xunit;
public class MyTest {
    public bool test(string path)
    {
        bool ret = File.Exists(path);
        if (!ret)
        {
            throw new FileNotFoundException("Not Found");
        }
        return ret;
    }
    [Fact]
    public void test_shouldSuccess()
    {
        string path = "anyKindOfPath must success";
        Shim shim = Shim.Replace(() => File.Exists(Is.A<string>())).With((string path) => true);

        var result = false;

        PoseContext.Isolate(() =>
        {
            result = test(path);
        }, shim);
        // assert
        Assert.True(result);
    }
}

We are ok that the File.Exists in test method is never call and be replace by the lambda wich is always true. When I run this simple code, I get the Exception "Not Found". The shim seems to be never called like expected. I run the 2.1 version of Pose downloaded from Github. The 2.1 version in Nuget crash with a "System.InvalidProgramException". I think this is the .Net Framework Version wich is incompatible with .net 5.0 Thank for your reply