Ambiguous match found exception in using Moq-Mock library
up vote
1
down vote
favorite
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.
c# inheritance mocking moq ambiguous
|
show 2 more comments
up vote
1
down vote
favorite
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.
c# inheritance mocking moq ambiguous
The classes show poor design that leads me to believe this is an XY problem. Why isFather
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 at 12:12
@Nkosi post updated.
– rejnev
Nov 22 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 at 12:24
@Nkosi if I have agetter
property that gets some data from request or ther sources, then I have more issues.
– rejnev
Nov 22 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 at 12:37
|
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
c# inheritance mocking moq ambiguous
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.
c# inheritance mocking moq ambiguous
c# inheritance mocking moq ambiguous
edited Nov 22 at 12:43
asked Nov 22 at 11:46
rejnev
1,0901429
1,0901429
The classes show poor design that leads me to believe this is an XY problem. Why isFather
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 at 12:12
@Nkosi post updated.
– rejnev
Nov 22 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 at 12:24
@Nkosi if I have agetter
property that gets some data from request or ther sources, then I have more issues.
– rejnev
Nov 22 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 at 12:37
|
show 2 more comments
The classes show poor design that leads me to believe this is an XY problem. Why isFather
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 at 12:12
@Nkosi post updated.
– rejnev
Nov 22 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 at 12:24
@Nkosi if I have agetter
property that gets some data from request or ther sources, then I have more issues.
– rejnev
Nov 22 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 at 12:37
The classes show poor design that leads me to believe this is an 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 at 12:12
The classes show poor design that leads me to believe this is an 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 at 12:12
@Nkosi post updated.
– rejnev
Nov 22 at 12:21
@Nkosi post updated.
– rejnev
Nov 22 at 12:21
1
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 at 12:24
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 at 12:24
@Nkosi if I have a
getter
property that gets some data from request or ther sources, then I have more issues.– rejnev
Nov 22 at 12:36
@Nkosi if I have a
getter
property that gets some data from request or ther sources, then I have more issues.– rejnev
Nov 22 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 at 12:37
Which is why you should show us what you are actually trying to do so we understand the actual problem.
– Nkosi
Nov 22 at 12:37
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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!
add a comment |
up vote
1
down vote
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; }
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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!
add a comment |
up vote
1
down vote
accepted
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!
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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!
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!
answered Nov 22 at 12:22
Fabjan
9,37421439
9,37421439
add a comment |
add a comment |
up vote
1
down vote
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; }
}
add a comment |
up vote
1
down vote
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; }
}
add a comment |
up vote
1
down vote
up vote
1
down vote
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; }
}
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; }
}
answered Nov 22 at 12:17
Rahul
61.7k114381
61.7k114381
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430330%2fambiguous-match-found-exception-in-using-moq-mock-library%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The classes show poor design that leads me to believe this is an 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 at 12:12
@Nkosi post updated.
– rejnev
Nov 22 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 at 12:24
@Nkosi if I have a
getter
property that gets some data from request or ther sources, then I have more issues.– rejnev
Nov 22 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 at 12:37