【发布时间】:2017-04-20 08:09:14
【问题描述】:
我有一个单例类,像这样:
public class XConnector : IXConnector
{
private static readonly Lazy<XConnector> instance =
new Lazy<XConnector>(() => new XConnector());
public static XConnector Instance => instance.Value;
private XConnector()
{
}
public async Task<XConnector> GetData(XConnector con)
{
}
}
如何使用 NSubstitute 模拟这个类?
另一方面:我想要这样的东西
var target = Substitute.For<IXConnector>();
这是我调试此代码时的快速观看
【问题讨论】:
-
有什么问题?如果
IXConnector是一个接口(我想),Substitute.For<IXConnector>();应该可以完美工作。 -
您可以使用 Mark Seemann 的 Ambient Context 模式。这将允许您注入一个实例,同时仍然提供单例的意图。
-
CrmConnector如何依赖XConnector? -
无论如何我认为
__target-member 不是你想的那样(但是不确定内部实现)。只需使用您已经拥有的代码var target = Substitute.For<IXConnector>();。这将为您提供接口的代理实例。但是它不知道你的单身人士的任何事情。也许你应该展示你是如何使用模拟的。 -
这里没有单例,只是一个涉及的接口实例。因此,请展示您使用
target所做的事情。
标签: c# .net unit-testing mocking nsubstitute