Calling Private Method

4 posts, 0 answers
  1. Rob
    Rob avatar
    2 posts
    Member since:
    Apr 2011

    Posted 09 May 2011 Link to this post

    How do I call a private method without using the MS accessor?  I find when using the accessor the mocking does not work,
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 13 May 2011 Link to this post

    Hi Rob,
    Thanks again for the bringing up the question. To mock private methods, you can take a look at the following link from online documentation that has quite a bit example to check out:

    http://www.telerik.com/help/justmock/advanced-usage-mocking-non-public-members-and-types.html

    Please let us know if you still having problems.

    Kind Regards,
    Ricky
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 03 Jun 2011 Link to this post

    I have the same issue as above. The problem I am having is that I am not mocking the private method but I am using the MSTest private accessor object in order to call the private method I am testing.  The mocked objects are public  here is some code to illustrate the issue.

    ///<summary>
          ///  A test for ProcessIncomingMessage
          ///</summary>
          [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\incoming.xml", "Event",  DataAccessMethod.Sequential), DeploymentItem("TestResources\\Unified\\incoming.xml"), TestMethod]
          public void ProcessIncomingMessageTest()
          {
              var expectedTower = TestContext.DataRow["Tower"].ToString();
              var expectedEventDescription = TestContext.DataRow["EventDescription"].ToString().TrimStart('\r', '\n', ' ');
              expectedEventDescription = expectedEventDescription.TrimEnd(' ', '\n', '\r', '\0');
              var rawDataToUse = Convert.FromBase64String(TestContext.DataRow["RawData"].ToString());
     
              var target = new UnifiedProtocolTranslator_Accessor(); // TODO: Initialize to an appropriate value
     
              bool called;
              Mock.Arrange(() => target.StoreMessages(Arg.IsAny<byte[]>(), Arg.AnyInt)).IgnoreArguments().DoInstead((byte[] arg1,int arg2) => called = true).MustBeCalled();
              Mock.Arrange(()=> target.ClearBuffers()).DoNothing().MustBeCalled();
              
              bool expected = true; // TODO: Initialize to an appropriate value
              bool actual;
              actual = target.ProcessIncomingMessage(rawDataToUse, rawDataToUse.Length);
              Assert.AreEqual(expected, actual);
              Mock.Assert(target);
             
          }

    When I call the ProcessIncomingMessage method I expect that StoreMessages and ClearBuffers are mocked, yet it goes and executes those methods as if they were not mocked.

    Thank you in advance.

  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 10 Jun 2011 Link to this post

    Hi Chris,

    Thanks for sending the issue. At this point, i would like to ask if StoreMessages and ClearBuffers are internal methods or these have been already invoked somewhere else (other test method) in the test class that you have used in the lines below:

    Mock.Arrange(() => target.StoreMessages(Arg.IsAny<byte[]>(), Arg.AnyInt)).IgnoreArguments().DoInstead((byte[] arg1,int arg2) => called = true).MustBeCalled();
     Mock.Arrange(()=> target.ClearBuffers()).DoNothing().MustBeCalled();


    Kind Regards,
    Mehfuz
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top