使用Moq测试对私有方法的调用 [英] Test calls to private methods with moq

查看:111
本文介绍了使用Moq测试对私有方法的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法需要通过Moq进行测试.问题在于switch语句中调用的每个方法都是私有的,包括末尾的PublishMessage.但是此方法(ProcessMessage)是公共的.如何测试此参数,以确保可以根据参数进行调用?注意,我不是在测试私有方法,我只是想测试调用".我想模拟这些私有方法,并检查是否使用安装程序调用它们,但是Moq不支持模拟私有方法.

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);
    }

推荐答案

您将不得不将那些私有方法更改为至少受保护的虚拟方法来模拟它们,然后使用模拟方法.Protected来模拟它们(http://blogs.clariusconsulting.net/kzu/mocking-protected-members-with-moq/).您不能嘲笑私有方法.

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(和其他一些框架)使用Castle Project的DynamicProxy在运行时动态生成代理,以便可以在不修改类代码的情况下拦截对象的成员.这种拦截只能在公共虚拟方法和受保护的虚拟方法上进行.

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.

有关更多信息,请参见下面的URL: http://www.castleproject.org/projects/dynamicproxy/

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

这篇关于使用Moq测试对私有方法的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆