Non-public methods not mocked

2 posts, 0 answers
  1. Alex
    Alex avatar
    6 posts
    Member since:
    Aug 2012

    Posted 24 Aug 2012 Link to this post

    This scenario unexpectedly throws "shouldn't be here" exception which means methods are not mocked out.  As soon as you change method access from protected to public, all works as expected.  It seems that only public methods get mocked out.  Is this by design?  I would expect all methods to be mocked out unless I specify Behavior.CallOriginal.

    public class Test1
    {
        protected void Testing1()
        {
            string s = "Testing1";
            throw new Exception("shouldn't be here");
        }
    }
     
    public class Test2 : Test1
    {
        protected void Testing2()
        {
            Testing1();
            string s = "Testing2";
            throw new Exception("shouldn't be here");
        }
    }
     
    public class Test3 : Test2
    {
        protected void Testing3()
        {
            Testing1();
            Testing2();
            string s = "Testing3";
        }
    }
     
     
        [Test]
        public void MakeSureAllMethodsAreMockedOut()
        {
            Test3 test3 = Mock.Create<Test3>();
     
            Mock.NonPublic.Arrange(test3, "Testing3").CallOriginal().MustBeCalled();
     
            test3.GetType().GetMethod("Testing3", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).Invoke(test3, new object[] { });
        }
    Here's simplified setup that also fails for the test above:
    public class Test3
    {
        protected void Testing1()
        {
            string s = "Testing1";
            throw new Exception("shouldn't be here");
        }
     
        public void Testing3()
        {
            Testing1();
            string s = "Testing3";
        }
    }
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 27 Aug 2012 Link to this post

    Hi Alex,
    Thanks again for contacting us.

    If you mock a class using Mock.Create by default it should mock all the public members. Generally, it leaves out the private ones as in most cases that lead to an increased performance while executing the tests. So your test will work just fine, if you do this:

    [Test]
    public void MakeSureAllMethodsAreMockedOut()
    {
        Test3 test3 = Mock.Create<Test3>();
     
        Mock.NonPublic.Arrange(test3, "Testing1").DoNothing();
        Mock.NonPublic.Arrange(test3, "Testing2").DoNothing();
     
        Mock.NonPublic.Arrange(test3, "Testing3").CallOriginal().MustBeCalled();
     
        test3.GetType().GetMethod("Testing3", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).Invoke(test3, new object[] { });
    }

    In short, JustMock is designed to mock non-public methods when needed.


    Kind Regards
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
Back to Top