views:

53

answers:

2

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);
}
+7  A: 

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.

Darin Dimitrov
Fair... see your point, but you didn't answer the question. Your name does not match the naming template (Product is not a method). So if I leave this out you name it Constructor_????_ShouldThrowIfNullArgumentSupplied. The question if valid if you do test first, then you need to start somewhere. And I often have a classes with no behavior, that could possible fail. Eg. :class Product(){ string Name {get; set;}}
Thomas Jespersen
Well then you see that there's a weakness in this naming convention :-)
Darin Dimitrov
:-) And that's the reason I ask the question. If you follow this naming convention then how do you name such tests. Right now I think you are right... don't write any test and allow entity which can not possible fail. I'm not a big fan of tests, and I'm trying to learn how to do "test first". So how would you write a test for a dum entity like I have.
Thomas Jespersen
Well maybe there's some other logic in this `Product` class that might be worth testing, like methods, but quite honestly writing a test for an empty constructor is pointless.
Darin Dimitrov
I do think that a new instance does call a method. I would warn you that following guidelines like this to the bitter end will be your ruin. So be forewarned, it is a guideline. A guideline is a way to keep you on the path. Paying attention to minutiae is not following the path.
Gutzofter
+1  A: 

Constructor_WithoutArguments_Succeeds

ndp