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

Allow setting up "overrides" for object members when mocking interfaces #248

Closed
kolomanschaft opened this issue Feb 15, 2016 · 4 comments · Fixed by #250 or #279
Closed

Allow setting up "overrides" for object members when mocking interfaces #248

kolomanschaft opened this issue Feb 15, 2016 · 4 comments · Fixed by #250 or #279

Comments

@kolomanschaft
Copy link

Consider the following mock of some interface:

var mock = new Mock<IMyThing>();
mock.Setup(s => s.Equals(It.IsAny<object>())).Returns<object>(x =>
{
    if (typeof(IMyThing).IsAssignableFrom(x.GetType()))
    {
        ...
    }
    return false;
});

var myThing = mock.Object;

There is no interceptor created for the Equals() method. But since I know every object implementing IMyThing will also be an object wouldn't it be nice if I was able to do this?

@kzu
Copy link
Contributor

kzu commented Aug 8, 2016

An unintended side effect of this feature: #273

:(

@adityap
Copy link

adityap commented Apr 7, 2017

Does this fix also include GetType() method calls?
mockSvc.Setup(svc => svc.GetType()).Returns(typeof(IMyService)); seems to be returning the Invalid setup on non-virtual error

@kolomanschaft
Copy link
Author

GetType is not virtual. Therefore it is not possible to mock it. But why would you need this anyway? Have you considered checking types in your code viaType.IsAssignableFrom(Type)?

@adityap
Copy link

adityap commented Apr 10, 2017

I am using .NET Core and was not sure if there is a way to call the method mentioned by you without calling GetType. Ref: http://stackoverflow.com/questions/41646964/asp-net-core-equivalent-of-type-isassignablefrom

However, I do get your point about it being not virtual.
Thanks!

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