Search code examples
c#unit-testingmicrosoft-fakes

Derived public method in class


I'm trying to test with Microsoft Fakes code in a application which sort of the following construction in a library:

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

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

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

I've created the following test for this in 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()

I get a null pointer when I call the Authenticate method from instance A.

Does anyone how I can solve this? I already looked and there is no overloaded constructor.


Solution

  • Solved this problem with the following code:

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

    This code ensures that when I call the Authenticate method from any class it will always return the specified value.