How to access a Static Class Private fields to unit test its methods using Microsoft Fakes in C# -
i have below static class , method in need unit test. able method has if condition uses boolean private variable , if value of false executes steps in if condition.
public static class logger { private static bool bnoerror = true; public static void log() { if (!bnoerror) { //then execute logic here } else { //else condition logic here } } }
is there way can set private field bnoerror value true can have 1 test method tests logic in if condition.
for unittesting purposes, microsoft has implemented few helper classes (privatetype , privateobject) use reflection scenarios this.
privatetype mytypeaccessor = new privatetype(typeof(typetoaccess)); mytypeaccessor.setstaticfieldorproperty("bnoerror", false);
privatetype intended static access, whereas privateobject testing against instantiated objects instead.
you need include microsoft.visualstudio.testtools.unittesting namespace microsoft.visualstudio.qualitytools.unittestframework.dll in order use these.
Comments
Post a Comment