C# reflection?
-
Hi, I have a class as under:
public class ExampleClass : BaseClass
{
public int ExampleMethod(int \_toValidate) { ExampleValidator(\_toValidate); return 1;
}
}
The ExampleValidator method is contained in the BaseClass. It is used to validate the parameter _toValidate. The ExampleValidator check is very important and every method in the class ExampleClass should contain it. I want to create a unit test for this class ExampleClass, to test if every method does contain the ExampleValidator. How can I do that? Can reflection help me? Regards, ap.
-
Hi, I have a class as under:
public class ExampleClass : BaseClass
{
public int ExampleMethod(int \_toValidate) { ExampleValidator(\_toValidate); return 1;
}
}
The ExampleValidator method is contained in the BaseClass. It is used to validate the parameter _toValidate. The ExampleValidator check is very important and every method in the class ExampleClass should contain it. I want to create a unit test for this class ExampleClass, to test if every method does contain the ExampleValidator. How can I do that? Can reflection help me? Regards, ap.
I think you are missing the point of unit testing. A unit test should run a piece of code and check that the output/result is as expected. A unit test does not examine code to check the semantics. What you should do is write several unit tests that run the ExampleMethod with different parameters and checked the results were correct. You would also write several unit tests that run the ExampleMethod with invalid parameters and check that the method failed in the correct way. What you are asking about doing is called "static analysis". MS have a tool called FxCop[^] which does static analysis. (Or if you have Vs2005/08 team versions, they have "code analysis" built in, which is basically the same as FxCop). If you google there are several tutorials for writing your own custom rules for FxCop. (see here[^]) There are plenty of other static analysis tools[^] if you want to try a few out. There is also DevPartner[^] from Compuware, which I've used in the past and you can add custom rules to.
Simon