Reputation: 96
I'm rather stuck on why I'm not getting 100% test branch coverage in my tests for this piece of code:
public List<ReturnItem> FilterItems(List<Items> items)
{
if (items== null || !items.Any())
{
throw new ArgumentException("No items to filter");
}
var newItems = new List<NewItem>();
foreach (var item in items)
{
if (item.Tracking.MidStateDate != null)
{
if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate)
{
var newItem = new NewItem(item);
newItem.MidStateDate = item.Tracking.MidStateDate.Value;
newItems.Add(newItem);
}
}
}
return newItems;
}
I've got the following tests:
I cannot get the branch coverage tests to 100%. Which makes me think I'm missing something. I've removed most of the code, and found that the issue is to do with this conditional if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate)
.
Can anyone suggest any other unit tests that I can add to handle the branch coverage issue?
Upvotes: 1
Views: 1039
Reputation: 96
I had a brainwave just as I replied to @juharr.
The issue was with the code not being explicit with the nullable datetime in the problematic conditional.
if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate)
^ Causes an issue
if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate.Value)
^ Works!
Upvotes: 2