.Net fakes. How to break through an inherited property when the base class is abstract?

I am trying to write unit test that spans the following line

var fileFullName = fileInfo.FullName; 

where fileInfo is an instance of FileInfo.

I use fakes to pin a FileInfo object, but I cannot provide a value for the FullName property because it is inherited from the base class.

For the Name property, which is not inherited, I can simply do this:

 ShimFileInfo.AllInstances.NameGet = info => OriginalFullName; 

Microsoft's answer is to create a base class gasket, in this case FileSystemInfo. But if I try this:

 ShimFileSystemInfo.AllInstances.FullNameGet = info => OriginalFullName; 

This does not work because FileSystemInfo is an abstract class that cannot be created and therefore cannot be customized.

In this particular case, I can get around it because I can combine the DirectoryName and Name properties to make it verifiable, but it seems crazy that I cannot just use the property that I want, because it comes from the database.

Has anyone come to this problem and managed to solve it?

+6
source share
1 answer

You said that shimming the base class does not work, but I do just that, and it works in our tests.

FileInfo in System.dll is defined as FileInfo : FileSystemInfo , and FileSystemInfo in mscorlib. Many types in mscorlib are not stripped by default, but if you add this to your mscorlib.fakes file:

 <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/"> <Assembly Name="mscorlib" Version="4.0.0.0"/> <ShimGeneration> <Add FullName="System.IO.FileSystemInfo"/> </ShimGeneration> </Fakes> 

and then create a test project, you will get ShimFileSystemInfo for FileSystemInfo from mscorlib, as well as ShimFileInfo for FileInfo from System.dll. Then it works:

 using (ShimsContext.Create()) { var directoryName = "<testPath>"; var fileName = "test.txt"; ShimFileSystemInfo.AllInstances.FullNameGet = @this => "42"; result = new DirectoryInfo(directoryName).GetFiles(fileName).First(); Assert.AreEqual("42", result.FullName); // the fake fullname Assert.AreEqual("test.txt", result.Name); // the real name } 

Caveat: works on my machine (Visual Studio 2013, .NET 4.5.1)

Counterfeit File Link: Coding, Compilation and Naming Conventions in Microsoft Fakes

+5
source

All Articles