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

Calling Private Method

3 Answers 233 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 09 May 2011, 05:23 PM
How do I call a private method without using the MS accessor?  I find when using the accessor the mocking does not work,

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 13 May 2011, 06:06 PM
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
0
Chris
Top achievements
Rank 1
answered on 03 Jun 2011, 03:28 PM
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.

0
Ricky
Telerik team
answered on 10 Jun 2011, 09:09 AM
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
Tags
General Discussions
Asked by
Rob
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Chris
Top achievements
Rank 1
Share this question
or