jeudi 1 janvier 2015

Testing code based on Third Party library with sealed classes

I have a class that interacts with Microsoft Tfs using the libraries included in Visual Studio.


I want to test my class but I had hard times trying to achieve it, due to the Tfs library architecture.


For example, most of their classes are Sealed or with internal only constructors, so it's very difficult to shim the default implementation.


This is a sample of the code I want to test:



public void Initialize()
{
workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(configuration.WorkspaceFullPath);
versionControlServer =newTfsTeamProjectCollection(workspaceInfo.ServerUri).GetService<VersionControlServer>();
workspace = versionControlServer.GetWorkspace(workspaceInfo);
}


I wrote this test method but I discovered that is not possible to create an instance of StubWorkspaceInfo.



[TestMethod]
public void GetCurrentChangeset()
{
Test(resolver =>
{
ShimWorkspaceVersionSpec.ConstructorWorkspace = (spec, workspace) => { };
ShimWorkstation.AllInstances.GetLocalWorkspaceInfoString = (workstation, s) => new StubWrkspaceInfo();
resolver.Resolve<SourceControl>().GetCurrentChangeset();
});
}


Any idea to get resolve this problem?


Aucun commentaire:

Enregistrer un commentaire