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 #128

Closed
MichaelLogutov opened this issue Aug 19, 2014 · 3 comments · Fixed by #145

Comments

@MichaelLogutov
Copy link

This code:

[TestClass]
public class Tests
{
    public interface IDataService
    {
        IList<int> GetData ();
    }


    [TestMethod]
    public void Test ()
    {
        // arrange
        var data_service_mock = new Mock<IDataService> ();
        data_service_mock.DefaultValue = DefaultValue.Mock;
        data_service_mock.CallBase = true;


        // act
        var data = data_service_mock.Object.GetData ();
        var result = data.Sum ();


        // assert
        Assert.IsTrue (result == 0);
    }
}

Throws:

Test method Bug.Tests.Test threw exception: 
System.NotImplementedException: This is a DynamicProxy2 error: The interceptor attempted to 'Proceed' for method 'System.Collections.Generic.IEnumerator`1[System.Int32] GetEnumerator()' 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)
    at Castle.DynamicProxy.AbstractInvocation.ThrowOnNoTarget()
   at Castle.DynamicProxy.Internal.CompositionInvocation.EnsureValidTarget()
   at Castle.Proxies.Invocations.IEnumerable`1_GetEnumerator.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Moq.Proxy.CastleProxyFactory.CallContext.InvokeBase()
   at Moq.InvokeBase.HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
   at Moq.Interceptor.Intercept(ICallContext invocation)
   at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IList`1Proxy.GetEnumerator()
   at System.Linq.Enumerable.Sum(IEnumerable`1 source)
   at Bug.Tests.Test() in Tests.cs: line 28

Moq v 4.2.1408.0717

The exception is not thrown when CallBase set to false which is strange because it should has no side effects when mocking interfaces.

It seems that this issue started to appear beginning version 4.2.1408.619.

@ploeh
Copy link
Sponsor

ploeh commented Aug 19, 2014

FWIW, I can confirm that I can reproduce this behaviour.

@sgrassie
Copy link

I just encountered this as well, which made a lot of red show up.

Following @ploeh's findings in AutoFixture/AutoFixture#303, I downgraded Moq to 4.2.1402.2112, and everything went green again.

jwChung added a commit to jwChung/ArticleHarbor-WebAPIDemo that referenced this issue Nov 4, 2014
rjasica pushed a commit to rjasica/moq4 that referenced this issue Nov 26, 2014
kzu added a commit that referenced this issue Feb 9, 2015
Fix: #128 NotImplementedException DynamicProxy2 error when CallBase = true
@LeonidLevin
Copy link

This seems to have been fixed.
Please, close the issue!

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

Successfully merging a pull request may close this issue.

5 participants