Ambiguous match found exception in using Moq-Mock library











up vote
1
down vote

favorite
1












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.










share|improve this question
























  • 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















up vote
1
down vote

favorite
1












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.










share|improve this question
























  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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
















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












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!





share|improve this answer




























    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; }
    }





    share|improve this answer





















      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',
      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
      });


      }
      });














      draft saved

      draft discarded


















      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








      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!





      share|improve this answer

























        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!





        share|improve this answer























          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!





          share|improve this answer












          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!






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 12:22









          Fabjan

          9,37421439




          9,37421439
























              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; }
              }





              share|improve this answer

























                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; }
                }





                share|improve this answer























                  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; }
                  }





                  share|improve this answer












                  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; }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 12:17









                  Rahul

                  61.7k114381




                  61.7k114381






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo