“Type”类型的扩展方法为null

时间:2013-07-19 13:04:50

标签: c# .net

我为System.Type创建了一个扩展方法,我想对该方法进行单元测试。这可能吗?谢谢!

public static bool GetFoo(this Type self)
{
     if (self == null)
     {
          throw new ArgumentNullException("this")
     }
     ...
}

4 个答案:

答案 0 :(得分:11)

断言((Type)null).GetFoo()抛出。

答案 1 :(得分:4)

MS VS单元测试

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestFoo()
{
    Type t = null;
    t.GetFoo();
}

答案 2 :(得分:1)

您也可以这样做:

ExtensionClass.GetFoo(null)

编辑:刚才注意到这与Jason Evans现在删除的答案相同......

答案 3 :(得分:0)

以其他方式延伸:

        this.GetType().GetFoo();//inside some class.
        typeof(SomeType).GetFoo();

因为它抛出了一个ArgumentNullException,你可以把它包装在一个合适的try-catch块中

相关问题