Using Moq to verify execution of private methods

那年仲夏 提交于 2019-12-01 18:02:54

Don't test private methods. They are private implementation details of the class. You should only test the results of executing public methods. As long as your results come out as expected, you shouldn't care how the result is obtained.

Building tests on private methods will lead to brittle tests that break easily when you refactor private implementations (for performance or other reasons).

Your class has two private utility methods that encapsulate some useful behavior, and the public method that you are testing must make use of this behavior. However, when you test, you don't want the normal behavior from these methods, you want to substitute a test behavior. What you have here is a classic case of dependency. When testing, dependecies within the class can be problematic.

So the solution is the same as for an external dependency: use dependency injection of one kind or another to delink the method you want to test from the private methods that implement the behavior. For instance, two private delegates can be declared to represent the behavior:

private Action Behavior1;
private Action Behavior2;

In the class constructor, the normal behavor is implemented thus:

public Foo (...)
{
    Behavior1 = privateMethod1;
    Behavior2 = privateMethod2;
...
}

In the public method the delegate is called instead of the actual method:

public void myPublicMethod(params) {
    if(some_condition)
        Behavior1();
    else
        Behavior2();
} 

By doing this the absolute dependency among the methods has been eliminated, so now it is testable.

So now, in the test, after the test object instance has been created, you can override the dependent behavior:

Foo_Accessor testMe = new Foo_Accessor();

bool wasCalled1 = false

testMe.Behavior1 = new Action(() => wasCalled1 = true);

...

Assert.IsTrue(wasCalled1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!