简体   繁体   中英

Unit Testing for a method that accepts MethodInfo objects

I need to unit tess a method that receives a MethodInfo object of some arbitrary user-code method (loaded dynamically via reflection).

public string SomeMethod(MethodInfo methodInfo);

I would like to exercise this method using different MethodInfo objects.

The simplest way is to use <type>.GetMethod("name") to retrieve a MethodInfo object, then call the tested method using it and assert against the result.

I would like to cover as many "different" method info's (or more specifically, as many different methods as possible to be passed in).

I am considering 2 approaches, not sure if this is how it should be done:

  1. Create a new test class with all methods i need on it. Use this type's GetMethods() and iterate those to feed them into my tested method.

  2. Create a separate unit test per method.

The first option is easier to extend (add a new method to the test class), but contains multiple asserts (asserts against each MethodInfo instance it has).

How can we easily solve this issue and create a robust test for this particular scenario?

Create a new test class with all methods i need on it. Use this type's GetMethods() and iterate those to feed them into my tested method.

If the logic in SomeMethod (which processes this MethodInfo) has generic logic which deals with all types of MethodInfo, then go by above route.

Create a separate unit test per method.

If the logic in SomeMethod has specific case by case logic to various types of MethodInfo, then go by above route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM