【发布时间】: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