0

在代码中,我需要检测,结果发现这样的外部依赖:如何通过Microsoft Fakes使用ConcurrentDictionary(C#)来长时间地依赖外部依赖?

var something = GConfig.SConfig[Type.ServiceType1].Names;

这部分的代码是这样的:

using System; 
using System.Collections.Concurrent; 
using System.Collections.Generic; 

namespace Class 
{ 
    public sealed class GConfig 
    { 

     public ConcurrentDictionary<Type, GConfigIt> SConfig { get; set; } 

     public GConfig() 
     { 
      SConfig = new ConcurrentDictionary<Type, GConfigIt>(); 
      throw new InvalidOperationException("Error"); 
     } 
    } 

    public enum Type 
    { 
     ServiceType1, 
     ServiceType2 
    } 

    public class GConfigIt 
    { 
     public List<string> Names { get; set; } 

     public GConfigIt() 
     { 
      Names = new List<string>(); 
     } 
    } 
} 

我需要垫片,它依赖,但对我自己的我无法找到完整的解决方案,只是部分:

对于GConfigIt(和匀场名称):Fakes.ShimGConfigIt.AllInstances.NamesGet

适用于垫片SConfig:Fakes.ShimGConfig.AllInstances.SConfigGet

但是我找不到连接,如何充分垫片。

P.S.我只是一个测试人员,不能改变现有的代码。为了做出改变,我需要说服开发人员这样做(即为GConfig提供额外的接口),但他们必须确保它不仅仅是用于“简单测试”或“测试测试”的改变,而且他们确实需要这样做。

回答

0

好的,所以你实际上是在正确的轨道上。您从Fakes.ShimGConfig.AllInstances.SConfigGet开始,一旦有效,您需要填写字典。

可能类似Fakes.ShimConcurrentDictionary.AllInstances.ItemGetType,你可以返回任何你想要的GConfigIt,可能是一个存根。

您可以设置所述存根的属性,也可以对名称执行之前的操作。

我假设并行字典可以被填充;如果没有,你仍然没有被卡住。只需将您的SConfigGet填充以返回一个SConfigGet的存根,您可以在其中输入有效值。这可能比其他方式更好,因为你对实现的承担较少。