【问题标题】:xUnit to replace thirdParty class instance with a fake onexUnit 用假的替换第三方类实例
【发布时间】:2018-11-21 09:21:09
【问题描述】:

我的部分课程将对象初始化为 MLM(需要大量设置和安装)我需要替换它 用一个假的对象以简单的方式做同样的事情,

例如如何使用假对象测试以下代码

// LMXProxyServerClass is the library in which need a lot of installation 
private readonly LMXProxyServerClass lmxProxyServer; 

这是我使用的一种方法的示例

private bool CreateBindingWithoutPropagate(string attributeName, bool supervisory)
{
    bool creatingBindingResult = false;

    lock (mxAccessProxyLock)
    {

        if (!bindings.ContainsKey(attributeName))
        {
            try
            {
                logger.Debug("Adding item " + attributeName + " to bindings, not in list so add.");

                // Add the handle to the mapping
                lmxHandleToAttributeNameMapping.Add(attributeBinding.MxAttributeHandle, attributeName);

                if (supervisory)
                {
                     lmxProxyServer.DoSOmethingElse(yyyyyyyyyyyyyyyyyyyyyyy);
                    logger.Info(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx);
                }
                else
                {

                    lmxProxyServer.DoSOmething(xxxxxxxx);
                    logger.Info(xxxxxxxxxxx);
                }

                // Add the binding to the list of bindings.
                bindings.Add(attributeName, attributeBinding);

                logger.Info("Creating binding for: " + attributeName);

                creatingBindingResult = true;

            }
            catch (System.UnauthorizedAccessException unauthorizedAccessException)
            {

                logger.Error("xxxxxxxxxxx", attributeName);
                throw ConvertExceptionToFault(unauthorizedAccessException);

            }
            catch (Exception ex)
            {
                throw ConvertExceptionToFault(ex);
            }
        }
    }

    return creatingBindingResult;
}

这个库是第三方库,所以我无法控制它,所以在测试中我需要用假的替换这个对象,这样我就不会在基本代码中做太多改动并简化其他部分的测试

【问题讨论】:

    标签: c# unit-testing xunit.net


    【解决方案1】:

    将代码与第 3 方实现关注点紧密耦合使得难以对代码进行孤立的单元测试。

    而是将 3rd 方实现关注点封装在一个抽象中,可以在测试时根据需要进行模拟。

    例如,创建第 3 方依赖项的抽象,仅公开代码所需的内容。

    public interface ILMXProxyServer {
        void DoSOmethingElse(...);
        void DoSOmething(...);
        //...
    }
    

    并通过构造函数注入将其显式注入依赖项。

    public class MyClass {
        private readonly ILMXProxyServer lmxProxyServer; 
    
        public MyClass(ILMXProxyServer lmxProxyServer) {
            this.lmxProxyServer = lmxProxyServer;
        }
    
        //...other code omitted for brevity
    }
    

    方法保持不变,因为它们将调用抽象的暴露成员。

    运行时实现将包装/封装第 3 方依赖项

    public class MyLMXProxyServerWrapper : ILMXProxyServer {
        // LMXProxyServerClass is the library in which need a lot of installation 
        private readonly LMXProxyServerClass lmxProxyServer; 
    
    
        public void DoSOmething(Something xxxxxxx){
             lmxProxyServer.DoSOmething(xxxxxxxx);
        }
    
        //...code omitted for brevity
    }
    

    通过该重构,代码现在更加灵活,能够在使用您选择的模拟框架或滚动您自己的特定于测试的实现进行隔离测试时模拟/伪造代理服务器。

    【讨论】:

    • 这是一个绝妙的主意,当我使用 topshelf 托管这个 wcf 服务时,我在想如何实现这一点并传递价值
    • @Ali Research 依赖倒置。如果无法使用 IoC 容器,那么 Pure DI 仍然可以创造奇迹。
    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多