gpt4 book ai didi

c# - xUnit 用假的替换第三方类实例

转载 作者:行者123 更新时间:2023-11-30 22:57:47 34 4
gpt4 key购买 nike

我的部分类(class)将对象初始化为 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;
}

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

最佳答案

将代码与第 3 方实现问题紧密耦合使得很难单独对代码进行单元测试。

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

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

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

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

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
}

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

关于c# - xUnit 用假的替换第三方类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408772/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com