- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的部分类(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/
我注意到一些 PHP 框架只使用小写字母 true/false 而其他框架则使用大写字母。 这有什么区别吗?我个人更喜欢小写字母。 最佳答案 没什么区别,有些人认为 FALSE 是常量,因此使用旧的尖
在我看来,以下两种映射方式实际上没有区别。以下是基于 @MapsId javadoc 的示例: // parent entity has simple primary key @Entity publ
我想知道是否有人知道编译器如何解释以下代码: #include using namespace std; int main() { cout << (true && true || false &
我想知道为什么alert(new Boolean(false))打印 false 而不是打印对象,因为 new Boolean 应该返回对象。如果我使用 console.log(new Boolean
即使在 verify=False 时,Python 请求也会给我一个 ssl 握手失败(我知道不使用 SSL 是不可取的)。对于其他具有有效证书的站点,请求按预期工作。我正在使用2.7。 from l
TDPL 描述了 assert(false); 语句的行为。此类断言不会从发布版本中删除(与所有其他断言一样),并且实际上会立即停止程序。问题是为什么?为什么会有如此令人困惑的行为?他们可能会添加 h
如何确定文档是否存在于 Meteor 的集合中? 编辑:新代码。 mongodb 有一个 ProductName 的文档:Apples 输入产品是“苹果” var exists = Products.
我想在 R 中做一些相当复杂的事情,但我不确定从哪里开始。 我有一个看起来像这样的数据框: main_val sub_val bit_one bit_two one a 1
我正在尝试编写一个函数 long(S1,S2) 如果 S1 比 S2 长,则该函数应该为真,否则为假。到目前为止,我所拥有的是以下内容: longer(A,nil). longer(nil,B) :-
我想知道我在这里缺少什么我有一个代码可以检查图像是否基于 Canvas 是透明的。 function Trasparent(url, npc, clb) { var img = new Ima
我想在按下按钮时使其他类框架设置可见(false)。 有一个名为 DisplayScore 的类,该类获取 getContentPane().add(new ScoreInfo());来自 Score
我正在使用 TableLayout 和 TableRow 创建 6/6 网格按钮。 private Button myButton; private static final int
这个问题没有实际用途!我之所以问这个只是因为我很好奇! 在C++中,有一种方法可以通过在某处编写#define true false来将true伪造为false,然后在代码中到处的true都将被视为f
我不久前学习 java,所以也许我有一个愚蠢的问题。 我有一张表,我在其中存储了一些信息。我想在此表中检查一些信息的可用性(这将只有一行),如果是 - 从这一行中获取特定列中的信息,如果没有 - 做另
这是一个 JavaScript 问题... 我正在尝试返回完全不区分大小写的匹配项。例如,如果我在输入框中输入 "yo",我希望它返回 true,这在我当前的方法中是这样做的,但它也会返回 true
我认为,我在 JS 中写了一个直接的 if 语句,但它运行不正确。 function printLetter(LetterId) { var studentflag = $("#IsStude
我如何使用 Javascript 执行以下操作? var object function() { return { object: Return true if object
我试图让导航栏在向上滚动时向下滑动,并在单击#menu-bar 时保持固定。但是当我再次单击同一个按钮时,我不知道如何在自动隐藏时将值改回 false,就在我有两个按钮的时候。非常感谢任何帮助! $(
下面的代码 #include using namespace std; int main(){ char greeting[50] = "goodmorning everyone";
我正在使用数据源将数据填充到我的数据 GridView 中。但是,我正在尝试找到一种方法让用户能够隐藏他不想看到的列。 我可以在程序运行之前隐藏和显示列: [Browsable(false)] pub
我是一名优秀的程序员,十分优秀!