c# - .Net Fakes - How to shim an inherited property when the base class is abstract? -
i trying write unit test covers following line
var filefullname = fileinfo.fullname;
where fileinfo instance of fileinfo.
i using fakes shim fileinfo object, unable provide value fullname property, because inherited base class.
for name property, not inherited, can this:
shimfileinfo.allinstances.nameget = info => originalfullname;
the answer provided microsoft create shim on base class, in case filesysteminfo. if try this:
shimfilesysteminfo.allinstances.fullnameget = info => originalfullname;
it not work, because filesysteminfo abstract class cannot created , therefore cannot shimmed.
in particular case, can around because can combine directoryname , name properties make testable, seems crazy can't use property want because happens come base.
has come accross problem , managed solve it?
you said shimming base class didn't work, , works in our tests.
fileinfo
in system.dll defined fileinfo : filesysteminfo
, , filesysteminfo
in mscorlib. many types in mscorlib not shimmed default, if add 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 build test project shimfilesysteminfo
filesysteminfo
mscorlib, shimfileinfo
fileinfo
system.dll. 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); // fake fullname assert.areequal("test.txt", result.name); // real name }
caveat: works on machine (visual studio 2013, .net 4.5.1)
reference fakes file: code generation, compilation, , naming conventions in microsoft fakes
Comments
Post a Comment