Difficulty in mocking private methods

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

    Posted 26 Apr 2016 Link to this post


    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

    public void ShouldInvokeNonPublicMember()
        Foo foo = new Foo();

        bool called = false;

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

        // Act

        // Assert

    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.




  2. Svetlozar
    Svetlozar avatar
    296 posts

    Posted 29 Apr 2016 Link to this post


    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.

    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
Back to Top