c# - How to call public static methods in Unit Test -
hi have simple method following , need know how can call in unit test in visual studio
public class myclass { public static bool test(string value, string regex) { if (regex.ismatch(value, regex, regexoptions.ignorecase)) return true; return false; } }
here have sofar
[testmethod] public void testmethod_test() { string value = "myvalue"; string regex = "&#@<>\s\\\$\(\)"; privatetype pt = new privatetype(typeof(myclass)); bool actualresult = (bool)pt.invokestatic("test", new object[] { value, regex }); bool expectedresult = false; assert.areequal(actualresult, expectedresult); }
you not want using reflection. call method directly:
[testmethod] public void testmethod_test() { string value = "myvalue"; string regex = "&#@<>\s\\\$\(\)"; var result = classcontainingtest.test(value, regex); assert.areequal(false, result); }
if classcontainingtest
isn't public, isn't sensible trying unit test test
. test publicly accessible functionality.
Comments
Post a Comment