简体   繁体   English

接受MethodInfo对象的方法的单元测试

[英]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). 我需要对一个方法进行单元设置,该方法接收某种任意用户代码方法(通过反射动态加载)的MethodInfo对象。

public string SomeMethod(MethodInfo methodInfo);

I would like to exercise this method using different MethodInfo objects. 我想使用不同的MethodInfo对象来执行此方法。

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. 最简单的方法是使用<type>.GetMethod("name")检索MethodInfo对象,然后使用它调用经过测试的方法并对结果进行断言。

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: 我正在考虑2种方法,不确定是否应该这样做:

  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. 使用此类型的GetMethods()并对其进行迭代,以将其输入到我的测试方法中。

  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). 第一个选项更易于扩展(向测试类添加新方法),但包含多个断言(针对其具有的每个MethodInfo实例进行断言)。

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. 使用此类型的GetMethods()并对其进行迭代,以将其输入到我的测试方法中。

If the logic in SomeMethod (which processes this MethodInfo) has generic logic which deals with all types of MethodInfo, then go by above route. 如果SomeMethod的逻辑(处理此MethodInfo的逻辑)具有处理所有类型的MethodInfo的通用逻辑,则按照上述路线进行操作。

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. 如果SomeMethod中的逻辑对各种类型的MethodInfo具有特定的案例逻辑, SomeMethod上述路线进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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