Difficulty in mocking private methods

2 posts, 0 answers
  1. Prasad
    Prasad avatar
    2 posts
    Member since:
    Apr 2015

    Posted 26 Apr Link to this post

    Hello,

    I am using JustMock with visual studio 2015 (just mock Q3 2015: 2015.3.929.5).

    I am getting method not found error when I mocked private method and asserting on private method call as following

    public class Foo
    {
        private void DoPrivate()
        {
            //Do something
        }

    }

    Following is test method

    [TestMethod]
    public void ShouldInvokeNonPublicMember()
    {
        Foo foo = new Foo();

        bool called = false;

        // Arrange
        Mock.NonPublic.Arrange(foo, "DoPrivate").DoInstead(() => called = true);

        // Act
        foo.DoPublic();

        // Assert
        Assert.IsTrue(called);
    }

    Here assert is always failing since DoInstead get not get triggered and method DoPrivate does not get called.

    I also tried with Mock.NonPublic.Arrange(foo, "DoPrivate").DoNothing().MustBeCalled(); After asserting this throws error as "DoPrivate()" method not found.

     

    Please suggest.

    Thanks,

    Prasad

     

  2. Svetlozar
    Admin
    Svetlozar avatar
    271 posts

    Posted 29 Apr Link to this post

    Hello,

    We couldn't reproduce the issue with JustMock 2015.3.929.5. Please note that your Foo type is missing DoPublic. I added a public DoPublic method that calls DoPrivate (assuming that is what your are trying to do) and the test passes on our side.

    Could you please send us a sample project that demonstrates the problem? Please, send us the exact error message you are getting on your side as well.

    Regards,
    Svetlozar
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
Back to Top