You should have a look at the Mirror<http://gallio.org/wiki/doku.php?id=mbunit:mirror>helper class. It provides many convenient functionalities to easily test private stuff.
Hope it helps, Yann. 2010/8/2 luc <[email protected]> > Hi! > > I have a question regarding testing private static methods. > Let's suppose I have a class like this > > public class DbUtils > { > static private String GenerateDbTestName(string filePath) > { > if (filePath == null) > throw new ArgumentNullException("filePath"); > if (String.IsNullOrEmpty(filePath)) > throw new ArgumentException("filePath"); > return "something"; > } > } > > I would like to test this method, so I do: > > public class DbUtilsTests > { > private Reflector reflector; > > [SetUp] > public void SetUp() > { > reflector = new > Reflector(typeof(DbUtils).Assembly.FullName, > typeof(DbUtils).FullName); > } > > [Test] > [Description("Przekazanie nieprawidłowych parametrów do metody > GenerateDbTestName.")] > public void > GenerateDbTestNameWithWrongParamsThrowsException_Test() > { > Assert.Throws<ArgumentNullException>(() => > reflector.InvokeMethod(AccessModifier.Static, "GenerateDbTestName", > null)); > Assert.Throws<ArgumentException>(() => > reflector.InvokeMethod(AccessModifier.Static, "GenerateDbTestName", > (object)"")); > } > } > > After running a test I receive: > > Test has failed. > The block threw an exception of a different type than was expected. > Expected Exception Type : System.ArgumentNullException > MbUnit.Framework.Reflection.ReflectionException: Fail to find > GenerateDbTestName Method in PMSLabs.Tester.DbUtils. > w MbUnit.Framework.Reflection.Reflector.EnsureMemberExists(Object > obj, Object member, String memberName, MemberType type) w c:\RelEng > \Projects\MbUnit v3.1\Work\src\MbUnit\MbUnit\Framework\Reflection > \Reflector.cs:wiersz 486 > etc. > > If I change the tested class to be static, the result is worst, > because a SetUp method throws > > Test has failed. > Failure to invoke test setup method > System.NullReferenceException > (...) > Stack: > c:\RelEng\Projects\MbUnit v3.1\Work\src\MbUnit\MbUnit\Framework > \Reflection\Reflector.cs(234, 13) : System.Object > CreateInstance(System.String, System.String, System.Object[]) > c:\RelEng\Projects\MbUnit v3.1\Work\src\MbUnit\MbUnit\Framework > \Reflection\Reflector.cs(43, 9) : Void .ctor(System.String, > System.String) > E:\(...)\DbUtilsTests.cs(21, 13) : Void SetUp() > > How is it possible to test private static methods? > > Thanks in advance for help. > Regards, > > Lucek > > -- > You received this message because you are subscribed to the Google Groups > "MbUnit.User" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<mbunituser%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/mbunituser?hl=en. > > -- You received this message because you are subscribed to the Google Groups "MbUnit.User" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/mbunituser?hl=en.
