2

I am using Moq and I realize In this situation I got the Ambiguous match found exception that I need help:

Here is my models:

public class User
{
}

public class CustomUser
{
}

Some classes:

public class BaseClass
{
    public virtual User User { get; set; }
}

public class Father : BaseClass
{
    public virtual new CustomUser User { get; set; }
}

public class Child : Father
{
}

And finally:

void Main()
{
    var user = new Mock<CustomUser>();
    var child = new Mock<Child>();
    child.SetupGet(x=>x.User).Returns (user.Object);  // Ambiguous match found.
}

Update:
Why am I using this?! Because I'm coding MVC-WebAPI and I have a BaseController which inherits the ApiController.
OK, in the ApiController we have a IPrincipal User property that I overrided it with my ICustomPrinciple implementation (this link).
Now I want to mock for example ProductController : BaseController.

var controller = new Mock<ProductController>();
var user = new Mock<CustomPrincipal>();

user.SetupGet(x => x.FullName).Returns("some full name");

controller.SetupGet(x => x.UserRoleID).Returns(81);// UserRoleID is getter and I do some stuff here.
controller.SetupGet(x => x.User).Returns(user.Object);

Any help will be appreciated.

Mohi
  • 1,776
  • 1
  • 26
  • 39
  • The classes show poor design that leads me to believe this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why is `Father` changing the base property? (explain so I can better understand what you are trying to do). Difficulty with testing something usually signals a problem with the original design – Nkosi Nov 22 '18 at 12:12
  • @Nkosi post updated. – Mohi Nov 22 '18 at 12:21
  • 1
    There should be no need to ever mock a controller. What are you actually trying to achieve? You should be able to create an instance of the controller and set the User. – Nkosi Nov 22 '18 at 12:24
  • @Nkosi if I have a `getter` property that gets some data from request or ther sources, then I have more issues. – Mohi Nov 22 '18 at 12:36
  • Which is why you should show us what you are actually trying to do so we understand the actual problem. – Nkosi Nov 22 '18 at 12:37
  • @Nkosi I didn't know `why am I using this` is important and I was avoiding to write not necessary codes, BTW I updated the post again. thanks. – Mohi Nov 22 '18 at 12:45
  • I gave a similar answer here https://stackoverflow.com/a/41589044/5233410 – Nkosi Nov 22 '18 at 12:47

2 Answers2

2

For mock to work it needs virtual property that in case of inheritance doesn't exist in base class (no ambiguity)

So you could rename the property as Rahul suggested or change the BaseClass to contain generic property:

public class BaseClass<TUser>
{
    public virtual TUser User { get; set; }
}

public class Father : BaseClass<CustomUser>
{
}

...
child.SetupGet(x=>x.User).Returns (user.Object);  // Works!
Fabjan
  • 13,506
  • 4
  • 25
  • 52
1

Why you are changing or forcefully hiding the base type and that's the issue here. If you want to define a separate member returning separate type then do it so like below and now your mock shouldn't complain anything when you say child.SetupGet(x => x.User1).Returns(user.Object);. You are changing the type of the property from User to Customuser and those two entities have no similarity between them.

public class Father : BaseClass
{
    public virtual CustomUser User1 { get; set; }
}
Rahul
  • 76,197
  • 13
  • 71
  • 125