Naming Unit Tests that just calls a constructor?
I'm trying to follow Roy Osherove's UnitTests naming convention, with the naming templ开发者_JAVA技巧ate: [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);
}
Constructor_WithoutArguments_Succeeds
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:
- You get an instance
- You get an exception
No need to test the first.
精彩评论