Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotImplementedException DynamicProxy2 error when CallBase = true while mocking interfaces again #582

Closed
ScotSalmon opened this issue Jan 24, 2018 · 3 comments · Fixed by #583
Labels

Comments

@ScotSalmon
Copy link

ScotSalmon commented Jan 24, 2018

This code:

    public class Tests
    {
        public interface IFoo
        {
            string Foo { get; }
        }

        public class Bar
        {
        }

        [Fact]
        public void Test()
        {
            // arrange
            var bar = new Mock<Bar>() { CallBase = true };
            bar.As<IFoo>();

            var foo = (IFoo)bar.Object;
            Mock.Get(foo).DefaultValue = DefaultValue.Mock; // this can also be done on nodeMock above, issue still reproduces in 4.8.1 and still doesn't in 4.8.0

            //Mock.Get(foo).SetupGet(obj => obj.Foo).Returns("foo"); // uncomment me to work around this issue

            // act
            var dummy = foo.Foo;
        }
    }

Throws:

Result StackTrace:
at Castle.DynamicProxy.AbstractInvocation.ThrowOnNoTarget()
at Castle.DynamicProxy.Internal.CompositionInvocation.EnsureValidTarget()
at Castle.Proxies.Invocations.IFoo_get_Foo.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Moq.CastleProxyFactory.Invocation.ReturnBase() in C:\projects\moq4\Source\ProxyFactories\CastleProxyFactory.cs:line 201
at Moq.Return.Handle(Invocation invocation, Mock mock) in C:\projects\moq4\Source\Interception\InterceptionAspects.cs:line 351
at Moq.Mock.Moq.IInterceptor.Intercept(Invocation invocation) in C:\projects\moq4\Source\Interception\Mock.cs:line 58
at Moq.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation) in C:\projects\moq4\Source\ProxyFactories\CastleProxyFactory.cs:line 176
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.BarProxy.get_Foo()
at ConsoleApplication1.Tests.Test() in C:\dev\ConsoleApplication1\ConsoleApplication1\Program.cs:line 30
Result Message: System.NotImplementedException : This is a DynamicProxy2 error: The interceptor attempted to 'Proceed' for method 'System.String get_Foo()' which has no target. When calling method without target there is no implementation to 'proceed' to and it is the responsibility of the interceptor to mimic the implementation (set return value, out arguments etc)

This is a close match to issue #128 but it's not the same issue -- the reproducing case posted there does not reproduce in 4.8.1.

Similar to what is described in #128, my code above works up through 4.2.1402, and throws the exception starting in 4.2.1408. Unlike the earlier issue which was apparently fixed in https://github.com/moq/moq4/releases/tag/v4.2.1502.911 my code continues throwing exceptions up through 4.7.145 (I did not check every release, but spot checked several in the range between 4.2.1408 and 4.8). It works in 4.8.0. It throws again in 4.8.1.

Uncommenting the explicit Mock.Get(foo).SetupGet(obj => obj.Foo).Returns("foo"); before acting on the mock makes it work in 4.8.1, but that wasn't necessary in 4.8.0, or before 4.2.1408.

@stakx
Copy link
Contributor

stakx commented Jan 25, 2018

Thanks for such a detailed error report, @ScotSalmon! I'll review this shortly.

@stakx
Copy link
Contributor

stakx commented Jan 25, 2018

@ScotSalmon - fixed. I didn't check old versions like you did, but it seems that this was a regression that has been around for a long time, and we didn't even know about it. Thanks for having put it back on the radar! The fact that your usage scenario worked briefly in 4.8.0 was due to yet another regression which got fixed in 4.8.1. I think the original regression from 4.2 times should be fixed now, too.

@stakx
Copy link
Contributor

stakx commented Feb 23, 2018

Moq 4.8.2 (which includes a patch for this issue, see changelog) has just been published on NuGet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants