Visual Studio 2013 is showing my code coverage for this (simplified for this example) object as missing a block:
As far as I know, that if
should have exactly two states. Pass and fail. And debugging my tests shows that each of those conditions is executed once. Specifically with these two tests:
[TestMethod]
public void CanNotHaveNegativeServiceWindow()
{
// arrange
var request = new CreateCaseRequest
{
ServiceWindowStart = new DateTime(2014, 12, 31, 12, 00, 00),
ServiceWindowEnd = new DateTime(2014, 12, 31, 11, 00, 00)
};
// act
var result = request.GetValidationErrors();
// assert
Assert.AreEqual(1, result.Count());
}
[TestMethod]
public void CanHaveServiceWindow()
{
// arrange
var request = new CreateCaseRequest
{
ServiceWindowStart = new DateTime(2014, 12, 31, 11, 00, 00),
ServiceWindowEnd = new DateTime(2014, 12, 31, 12, 00, 00)
};
// act
var result = request.GetValidationErrors();
// assert
Assert.AreEqual(0, result.Count());
}
One test validates the positive result of that specific if
condition, the other validates the negative result. What block isn't covered? What logical condition exists that I'm missing?
When you compare Nullable<T>
values, C# compiler creates additional checks to see that the Nullable<T>
s have values. These checks will always come out the same way in your code, because you have already done all the null
checking explicitly.
Changing the condition to
if (ServiceWindowStart.Value > ServiceWindowEnd.Value)
should fix this problem.