I'm working on some test cases at the moment, and I'm regularly finding that I'm ending up with multiple asserts in each case. For example (over-simplified and comments stripped for brevity):
[Test]
public void TestNamePropertyCorrectlySetOnInstantiation()
{
MyClass myInstance = new MyClass("Test name");
Assert.AreEqual("Test Name", myInstance.Name);
}
This looks acceptable in principle, but the point of the test is to verify that when the the class is instantiated with a given name, the Name property is set correctly, but it fails if anything goes wrong on instantiation, before it even gets to the assertion.
I refactored it like this:
[Test]
public void TestNamePropertyCorrectlySetOnInstantiation()
{
MyClass myInstance;
string namePropertyValue;
Assert.DoesNotThrow(() => myInstance = new MyClass("Test name"));
Assert.DoesNotThrow(() => namePropertyValue = myInstance.Name);
Assert.AreEqual("Test Name", namePropertyValue);
}
but of course, now I'm actually testing three things here; In this test, I'm not interested in testing whether or not the instance of MyClass was successfully instantiated, or that the Name property was read successfully, these are tested in another case. But how can I test the last assertion without asserting the other two first, given that it's not possible to even do the test if the first two fail?