简体   繁体   English

如何测试使用Activator.CreateInstance(…)的方法?

[英]How to test a method that uses Activator.CreateInstance(…)?

I'm writing a test for the following method: 我正在为以下方法编写测试:

public IAnInterface Instantiator()
{
    var type = Type.GetType(A_CONSTANT_STRING);
    return (IAnInterface)Activator.CreateInstance(type);
}

When the test is running, the type obtained from the Type.GetType(...) method is null. 运行测试时,从Type.GetType(...)方法获得的类型为null。 My questions are: 我的问题是:

  1. The null is being returned because not all the types are loaded(?) when the tests are executed? 返回null是因为执行测试时并非所有类型都已加载(?)?
  2. How to overcome this problem? 如何克服这个问题? Actually I want to test if the required type has been returned. 实际上,我想测试是否已返回所需的类型。

Thanks in advance 提前致谢

As @Krekkon already mentioned Type.GetType() method requires the following string: 正如@Krekkon已经提到的那样, Type.GetType()方法需要以下字符串:

The assembly-qualified name of the type to get. 要获取的类型的程序集限定名称。 See AssemblyQualifiedName. 请参阅AssemblyQualifiedName。 If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace. 如果类型在当前正在执行的程序集中或在Mscorlib.dll中,则只需提供其名称空间限定的类型名称即可。

For question #2: You will not be able to test whether the type has been returned because of the static call to Type.GetType() . 对于问题2:由于对Type.GetType()的静态调用,您将无法测试是否已返回类型。 If you can change the code you could introduce an interface that is responsible for type resolving and represents a facades of the static GetType method call: 如果可以更改代码,则可以引入一个接口,该接口负责类型解析并表示静态GetType方法调用的外观:

interface ITypeResolver
{
  Type GetType(string typeName);
}

class SomeClass
{
  private readonly ITypeResolver typeResolver = ...;

  public IAnInterface Instantiator()
  {
    var type = this.typeResolver.GetType(A_CONSTANT_STRING);
    return (IAnInterface)Activator.CreateInstance(type);
  }
}

Using an interface you are able to mock the ITypeResolver and also test the implementation of ITypeResolver interface in Isolation. 使用接口,您可以模拟ITypeResolver ,也可以在Isolation中测试ITypeResolver接口的实现。

var type = Type.GetType("namespace.qualified.TypeName");

通过添加对在A_CONSTANT_STRING指定的类型所在的项目的引用来解决。

What assembly is your type defined in? 您的类型定义在哪个程序集中? Assuming your method is in the same assembly as the type you are looking for, try: 假设您的方法与要查找的类型在同一程序集中,请尝试:

var type = Assembly.GetExecutingAssembly().GetTypes()
    .First(t => t.Name == A_CONSTANT_STRING);

return (IAnInterface)Activator.CreateInstance(type);

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

相关问题 为什么该方法使用 Activator.CreateInstance 而不是直接使用“new Class()”? - Why the method uses Activator.CreateInstance instead of directly "new Class()"? 如何调用Activator.CreateInstance,传递方法作为构造函数参数? - How to Call Activator.CreateInstance, passing a Method as Constructor Argument? 如何将 IHttpContextAccessor 作为参数传递给 Activator.createinstance 方法中的创建实例? - How to Pass IHttpContextAccessor as a parameter for crating instance in Activator.createinstance method? 是否可以使用Activator.CreateInstance()模拟方法? - Is it possible to mock a method with using of Activator.CreateInstance()? 创建一个方法的DeclaringType实例:Activator.CreateInstance - Create an instance of the DeclaringType of a method: Activator.CreateInstance 如何将参数传递给 Activator.CreateInstance<T> () - How to Pass Parameters to Activator.CreateInstance<T>() 如何为各种构造函数做Activator.CreateInstance? - How to do Activator.CreateInstance for various constructors? 如何为Activator.CreateInstance选择“正确”的构造函数 - How to chose the “right” constructor for Activator.CreateInstance Activator.CreateInstance MissingMethodException - Activator.CreateInstance MissingMethodException Activator.CreateInstance() - Activator.CreateInstance()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM