lionheart
lionheart

Reputation: 433

Extension method which this of type "Type" is null

I created an extension method for System.Type and I would like to unit test that method. Is this possible? Thanks!

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

Upvotes: 0

Views: 108

Answers (4)

terrybozzio
terrybozzio

Reputation: 4542

Just extending with other ways:

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

Since it throws an ArgumentNullException you can wrap it inside a proper try-catch block

Upvotes: 0

Denis
Denis

Reputation: 6082

MS VS Unit Test

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

Upvotes: 3

leppie
leppie

Reputation: 117230

You can do this too:

ExtensionClass.GetFoo(null)

Edit: Just noted this is the same as Jason Evans's now deleted answer...

Upvotes: 1

user1228
user1228

Reputation:

Assert that ((Type)null).GetFoo() throws.

Upvotes: 11

Related Questions