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

Return default value for not implemented methods for As<> method #609

Closed
pluginbb opened this issue Mar 28, 2018 · 2 comments
Closed

Return default value for not implemented methods for As<> method #609

pluginbb opened this issue Mar 28, 2018 · 2 comments

Comments

@pluginbb
Copy link

Hello!

I'm trying to create mock for IServiceInterface with implementations for several methods.
Hovewer my DefaultImplementationClass not implemented all methods from IServiceInterface, so when I call methods that don't exists in DefaultImplementationClass program throws exception like:

System.NotImplementedException: This is a DynamicProxy2 error: The interceptor attempted to 'Proceed' for method 'MethodThatNotExistsInDefaultImplementationClass(Int64)' 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)

Example of my code:

var mock = new Mock<DefaultImplementationClass>().As<IServiceInterface>();
mock.Callbase = true;

mock.Object.MethodThatExistsInDefaultImplementationClass(); // all good, calling real implementation
mock.Object.MethodThatNotExistsInDefaultImplementationClass(); // throws exception

So the question, can I somehow make it so that it simply returns the default value,
not to throw exception in this case?
thanks in advance

@stakx
Copy link
Contributor

stakx commented Mar 28, 2018

Hi @pluginbb, could you please post minimal & self-contained reproduction code so we can take a closer look at this?

@pluginbb
Copy link
Author

@stakx
sorry, when I tried to reproduce this error, I created a new project with latest version of Moq, and everything worked out! I though that I have updated Moq to latest version in my real project, my mistake =(
Now everything is works, thank you very much!

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

No branches or pull requests

2 participants