tonerdo / pose

Replace any .NET method (including static and non-virtual) with a delegate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception in shim delegate doesn't respect try/catch in isolated method

dtkujawski opened this issue · comments

Here is some code where MethodA calls MethodB - if there is no exception, MethodA returns "0" otherwise it returns "1". However, if I make a shim for MethodB that calls an exception, Pose execution stops and the exception isn't caught in MethodA.

Is there a way to throw exceptions in shims that are caught in the isolated method?

        [Fact]
        public void Test()
        {
            int i = MethodA();
            Assert.Equal(0, i);

            PoseContext.Isolate(() =>
            {
                i = MethodA();
            }, Shim.Replace(() => MethodB()).With(() => { throw new Exception("test"); }));
            Assert.Equal(1, i);
        }

        public static int MethodA()
        {
            try
            {
                MethodB();
            }
            catch
            {
                return 1;
            }
            return 0;
        }

        public static void MethodB()
        {
        }

Hi @dtkujawski, Pose currently doesn't seem to handle try...catch blocks quite well. Will look into it

Hi @dtkujawski #31 added the fix to respect exception handling in methods

Thank you for the fix! This works great.