This is a migrated thread and some comments may be shown as answers.

I cant grant access to private generic method

2 Answers 100 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Rommel Meza
Top achievements
Rank 1
Rommel Meza asked on 26 May 2011, 05:39 PM
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 Answers, 1 is accepted

Sort by
0
Rommel Meza
Top achievements
Rank 1
answered on 26 May 2011, 06:31 PM
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; }
    }
0
Ricky
Telerik team
answered on 03 Jun 2011, 09:58 AM
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 >>

Tags
JustMock Free Edition
Asked by
Rommel Meza
Top achievements
Rank 1
Answers by
Rommel Meza
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or