Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Unit Testing - Should you unit test something in a derived class that is taken care of in a base class?

public class Foo<T>
{
    public Foo(Bar bar)
    {
        if (bar == null) throw new ArgumentNullException("bar");
    }
}

public class Foo : Foo<Object>
{
    public Foo(Bar bar) : base(bar) { }
}

Basically, I understand that I should unit test the constructor for the Generic Foo. Should I also unit test the constructor for the non-generic version? The reason I ask is because the exception is being thrown at the generic constructor level...

[TestMethod]
[ExpectedExeption(typeof(ArgumentNullException))]
public void GenericFooNullArgumentInConstructor()
{
    var foo = new Foo<int>(null);
}

//Is this test necessary?
[TestMethod]
[ExpectedExeption(typeof(ArgumentNullException))]
public void NonGenericFooNullArgumentInConstructor()
{
    var foo = new Foo(null);
}
like image 528
michael Avatar asked Oct 11 '11 14:10

michael


1 Answers

Yes. Without looking at all the implementations, that's the easiest way to verify that your derived classes have maintained the guarantees of your base class. Especially when Bob the intern comes in later and modifies your derived class to do something else.

like image 81
Ilian Avatar answered Nov 06 '22 08:11

Ilian