如何为ArgumentNullException单元测试重载的构造函数,其中每个构造函数只有一个参数

Joh*_*ohn 1 c# unit-testing visual-studio-2013

我正在测试一个具有重载构造函数的类,第一个采用单个实体,第二个采用相同类型实体的列表.

class MyClass
{
      public MyClass(Entity entity)
      {
           if(entity == null) throw new ArgumentNullException("entity");

           // continue initialising.
      }

      public MyClass(IList<Entity> entityList)
      {
            if(entityList == null) throw new ArgumentNullException("entityList");

            // continue initialising.
      }
}
Run Code Online (Sandbox Code Playgroud)

但是当然尝试通过传递null来测试每个构造函数会导致我想要通过单元测试调用哪个构造函数的模糊性.

这有什么办法吗?

Lee*_*Lee 5

您可以强制转换null以消除歧义:

var c = new MyClass((Entity)null);
var cl = new MyClass((IList<Entity>)null);
Run Code Online (Sandbox Code Playgroud)