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

Difficulty in mocking private methods

1 Answer 27 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Prasad
Top achievements
Rank 1
Prasad asked on 26 Apr 2016, 11:16 AM

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

 

1 Answer, 1 is accepted

Sort by
0
Svetlozar
Telerik team
answered on 29 Apr 2016, 08:51 PM
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
Tags
General Discussions
Asked by
Prasad
Top achievements
Rank 1
Answers by
Svetlozar
Telerik team
Share this question
or