在类中派生的公共方法

时间:2014-09-01 15:16:41

标签: c# unit-testing microsoft-fakes

我试图在应用程序中使用Microsoft Fakes代码进行测试,该应用程序在库中具有以下结构:

public static class C
{
   public A GetConfigurationFactory(Uri uri);
}

public class A : B
{
   public A(Uri uri)
   {
   }
}

public class B
{
    public void Authenticate()
    {
    }
}

我在Visual Studio中为此创建了以下测试:

//Arrange
ShimA.ConstructorUri = (@this, value) => 
{ 
   var shim = new ShimA(@this); 
};        

ShimC.GetConfigurationFactory = (uri) => 
{ 
   var shim = new A(uri);
   return shim;
};

var uri1 = new Uri("http://test-url");

//Act
A.Authenticate()

当我从实例null调用Authenticate方法时,我得到一个A指针。

有谁可以解决这个问题?我已经看了,没有重载的构造函数。

1 个答案:

答案 0 :(得分:0)

使用以下代码解决了这个问题:

ShimB.AllInstances.Authenticate = (someValue) => { return; }

此代码确保当我从任何类调用Authenticate方法时,它将始终返回指定的值。