Ambiguous match found exception in using Moq-Mock library
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
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 '18 at 12:12
@Nkosi post updated.
– Bagherani
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 agetter
property that gets some data from request or ther sources, then I have more issues.
– Bagherani
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
|
show 2 more comments
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 '18 at 12:43
Bagherani
asked Nov 22 '18 at 11:46
BagheraniBagherani
1,21711430
1,21711430
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 '18 at 12:12
@Nkosi post updated.
– Bagherani
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 agetter
property that gets some data from request or ther sources, then I have more issues.
– Bagherani
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
|
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 '18 at 12:12
@Nkosi post updated.
– Bagherani
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 agetter
property that gets some data from request or ther sources, then I have more issues.
– Bagherani
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
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 '18 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 '18 at 12:12
@Nkosi post updated.
– Bagherani
Nov 22 '18 at 12:21
@Nkosi post updated.
– Bagherani
Nov 22 '18 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 '18 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 '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.– Bagherani
Nov 22 '18 at 12:36
@Nkosi if I have a
getter
property that gets some data from request or ther sources, then I have more issues.– Bagherani
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
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
|
show 2 more comments
2 Answers
2
active
oldest
votes
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 |
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 12:22
FabjanFabjan
10.5k31639
10.5k31639
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:17


RahulRahul
62.7k124482
62.7k124482
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.
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 '18 at 12:12
@Nkosi post updated.
– Bagherani
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.– Bagherani
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