Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Unit Tests that just calls a constructor?

I'm trying to follow Roy Osherove's UnitTests naming convention, with the naming template: [MethodName_StateUnderTest_ExpectedBehavior].

Following this pattern. How would you name a test calling a constructor?

[Test]
public void ????()
{
    var product = new Product();
    Assert.That(product, Is.Not.Null);
}
like image 457
Thomas Jespersen Avatar asked May 15 '10 16:05

Thomas Jespersen


2 Answers

Constructor_WithoutArguments_Succeeds

like image 105
ndp Avatar answered Nov 07 '22 11:11

ndp


I don't know how you would call this unit test but I strongly suggest you avoid writing it as there's nothing on earth to make the assert fail. If the constructor succeeds the CLR guarantees you a fresh instance which won't be null.

Now if the constructor of your object throws an exception under some circumstances you could name it like this:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Product_Constructor_ShouldThrowIfNullArgumentSupplied()
{
    new Product(null);
}

So two possible cases for the code you are testing:

  1. You get an instance
  2. You get an exception

No need to test the first.

like image 32
Darin Dimitrov Avatar answered Nov 07 '22 11:11

Darin Dimitrov