I cant grant access to private generic method

3 posts, 0 answers
  1. Rommel
    Rommel avatar
    2 posts
    Member since:
    Mar 2008

    Posted 26 May 2011 Link to this post

    Hi im try to grant access to a private method with class generic type return but allway throw the same error. i put here and example only the structure as my code.

    Alway send the error:
    Argument Exception
    
    Could not resolve the target method; make sure that you have provided arguments correctly.
    
    Code:

        public interface IBase
        {
        }
     
        public abstract class BaseClass
        {
        }
     
        public class AssignedClass : BaseClass
        {
        }
     
        public class WrapperClass<T> : IBase where T : BaseClassnew()
        {
        }
     
        public class Control
        {
            private WrapperClass<AssignedClass> getWrapper()
            { return null; }
        }
     
        [TestClass]
        public class TestFlow
        {
            [TestMethod]
            public void ShouldWork()
            {
                var control = Mock.Create<Control>();
                var wrapper = Mock.Create<WrapperClass<AssignedClass>>();
     
                //Dosent Work
                Mock.NonPublic.Arrange<WrapperClass<AssignedClass>>(control, "getWrapper").Returns(wrapper);
     
                //Dosent Work
                Mock.NonPublic.Arrange<IBase>(control, "getWrapper").Returns(wrapper);
            }
     
        }
  2. Rommel
    Rommel avatar
    2 posts
    Member since:
    Mar 2008

    Posted 26 May 2011 Link to this post

    Its done. the only thing that i needed is chage to protected the function and its work.

    This

        public class Control
        {
            private WrapperClass<AssignedClass> getWrapper()
            { return null; }
        }


    To This

        public class Control
        {
            protected virtual WrapperClass<AssignedClass> getWrapper()
            { return null; }
        }
  3. DevCraft R3 2016 release webinar banner
  4. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 03 Jun 2011 Link to this post

    Hi Rommel,
    Great that you made things work for you. However, your previous test was also correct and we recently fixed a bug with NonPublic resolve with which I tried the following and it worked:


    [TestClass]
         public class TestFlow
         {
             [TestMethod]
             public void ShouldWork()
             {
                 var control = Mock.Create<Control>(Behavior.CallOriginal);
                 var wrapper = Mock.Create<WrapperClass<AssignedClass>>();
                 //Dosent Work
                 Mock.NonPublic.Arrange<WrapperClass<AssignedClass>>(control, "getWrapper").Returns(wrapper).MustBeCalled();
                 control.Load();
                 Mock.Assert(control);
             }
         }


    Here i added the following method in Control class that is used to confirm mock above:

    public class Control
    {
        private WrapperClass<AssignedClass> getWrapper()
        { return null; }
        public void Load()
        {
            getWrapper();
        }
    }


    If you required this fix, please also let us know (in that case you need to open a support ticket by which we will send you the patch).

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Back to Top