Moles does not work in static constructors -
i have been having problem mole types not working in static constructors. have created 2 simple examples explain problem:
i have simple instance class follows:
public class instancetestreader { public instancetestreader () { ifilesystem filesystem = new filesystem(); this.content = filesystem.readalltext("test.txt"); } public string content { get; private set; } }
i have unit test follows:
[testmethod] [hosttype("moles")] public void checkvalidfileinstance_withmoles() { // arrange string expectedfilename = "test.txt"; string content = "test text content"; implementation.moles.mfilesystem.allinstances.readalltextstring = (moledtarget, suppliedfilename) => { assert.areequal(suppliedfilename, expectedfilename, "the filename incorrect"); return content; }; // act string result = new instancetestreader().content; // assert assert.areequal(content, result, "the result incorrect"); }
this works no problems.
if change calling class static (not moled class, calling class), moles no longer works:
public static class statictestreader { static statictestreader () { ifilesystem filesystem = new filesystem(); content = filesystem.readalltext("test.txt"); } public static string content { get; private set; } }
and modify unit test accordingly:
[testmethod] [hosttype("moles")] public void checkvalidfilestatic_withmoles() { // arrange string expectedfilename = "test.txt"; string content = "test text content"; implementation.moles.mfilesystem.allinstances.readalltextstring = (moledtarget, suppliedfilename) => { assert.areequal(suppliedfilename, expectedfilename, "the filename incorrect"); return content; }; // act string result = statictestreader.content; // assert assert.areequal(content, result, "the result incorrect"); }
... moles no longer works. when running test, error "could not find file 'd:\blah\blah\test.txt'". because moles no longer in charge of filesystem class, unit test calling original implementation looking file on file system.
so, change class moled method called static. have not changed moled class or method, still instance types, cannot use implementation.moles.sfilesystem syntax mocking static class.
please explain how can moles work within static method/constructor?
many thanks!!!
a static constructor different static method. method have control of when , called. constructor automatically called runtime before access class performed in case leads constructor being called before mole filesystem
set resulting in error seeing.
if change implementation following should work.
public static class statictestreader { private static string content; public static string content { { if (content == null) { ifilesystem filesystem = new filesystem(); content = filesystem.readalltext("test.txt"); } return content; } } }
update:
however, if cannot change implementation other alternative moles offers prevent code in static constructor executed , mole content
property directly. erase static constructor type need include following assembly level attribute in test assembly:
[assembly: moleserasestaticconstructor(typeof(statictestreader))]
Comments
Post a Comment