Test calls to private methods with moq

隐身守侯 提交于 2020-01-01 09:19:15

问题


I have the following method I need to test with Moq. The problem is that each method called in the switch statement is private, including the PublishMessage at the end. But this method (ProcessMessage) is public. How can I test this so that I can ensure the calls are made depending on the parameter? Note that I'm not testing the private methods, I just want to test the "calls". I'd like to mock these private methods and check if they are called using Setup, but Moq does not support mocking private methods.

public void ProcessMessage(DispenserMessageDataContract dispenserMessage)
    {
        var transOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted };
        using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, transOptions))
        {
            switch (dispenserMessage.Type)
            {
                case DispenserMessageType.AckNack:
                    UpdateAckNackMessageQueue(dispenserMessage);
                    break;

                case DispenserMessageType.FillRequest:
                    CreateFillRequestMessageQueue(dispenserMessage);
                    break;

                case DispenserMessageType.FillEvent:
                    UpdateFillEventMessageQueue(dispenserMessage);
                    break;

                case DispenserMessageType.RequestInventory:
                    CreateRequestInventoryMessageQueue(dispenserMessage);
                    break;

                case DispenserMessageType.ReceiveInventory:
                    CreateReceiveInventoryMessageQueue(dispenserMessage);
                    break;
            }

            scope.Complete();
        }

        PublishMessage(dispenserMessage);
    }

回答1:


You will have to change those private methods to atleast protected virtual to mock them and then use mock.Protected to mock them(http://blogs.clariusconsulting.net/kzu/mocking-protected-members-with-moq/). You can't mock private methods.

Moq (and few other frameworks) uses Castle Project's DynamicProxy to generate proxies on the fly at run-time so that members of an object can be intercepted without modifying the code of the class. That interception can only be done on public virtual and protected virtual methods.

See below URL for more information: http://www.castleproject.org/projects/dynamicproxy/




回答2:


You could extract the private method in another class and make them public, then mock those with Moq and verify that they have been called.




回答3:


Moq is for mocking properties and methods declared in interfaces and or abstract properties and methods in classes.

The idea behind Moq-testing is that you test the interactions between your class-under-test and the rest of the world (its dependencies). Moq does this by creating a "mocked" implementation of the interface or a derivative of the abstract class with the abstract methods implemented.

Moq cannot override existing implementation like your private methods. This is not how Moq works.

Either you should test "ProcessMessage" with all possible input and expected output or you should refactor your class to delegate the calls to interface methods that you can mock with Moq. Testing private methods is a bad concept anyway. They are kept private for a reason, which is to hide the implementation such that it can change at will.




回答4:


I prefer to add additional class (*Helper) and move on all my private methods to public. Then you can easily test your methods directly. I didn't find more elegant way to do that.




回答5:


In some cases, you may need to alter the behavior of private method inside the class you are unit testing. You will need to mock this private method and make it return what needed for the particular case. Since this private method is inside your class under test then mocking it is little more specific. You have to use spy object.

Spy object A spy is a real object which mocking framework has access to. Spied objects are partially mocked objects. Some their methods are real some mocked. I would say use spy object with great caution because you do not really know what is happening underneath and whether are you actually testing your class or mocked version of it.

public class PowerMockDemo 
{

    public Point callPrivateMethod() {
        return privateMethod(new Point(1, 1));
    }

    private Point privateMethod(Point point) {
        return new Point(point.getX() + 1, point.getY() + 1);
    }
}

Then you will mock the Spy object

Hope that will help you,

Best wishes



来源:https://stackoverflow.com/questions/21004323/test-calls-to-private-methods-with-moq

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