I have a problem with Visual Studio 2012 unit testing, more precisely – with code coverage.
Here is a target code:
var someDeals = allDeals
.Where(i => i.Number != null && !i.Number.StartsWith(SomeString))
.Select(i => new DealInfoDto
{
CurrentDebtAmount = i.CurrentDebtAmount,
Date = i.Date,
NextMonthlyPaymentDate = i.NextMonthlyPaymentDate,
Number = i.Number,
AccountNumber = i.AccountNumber,
})
.ToArray();
When I run analyze code coverage it shows me that lambda expression in second line (i.Number != null && !i.Number.StartsWith(SomeString)) is partially touched area and I can’t get 100% coverage. But I’m sure that this expression is evaluated (expression inside SELECT is OK, so expression inside WHERE is true and that’s mean that both parts of && statement were touched, also I’ve checked data and I’m sure that i.Number != null and !i.Number.StartsWith(SomeString).
Why this block is not covered?
P.S. I splitted Where into two expressions and everything became OK. But it looks smth ugly and would be better just one Where:
allDeals
.Where(i => i.Number != null)
.Where(i => !i.Number.StartsWith(CardAccountIdentifier))
This question looks very similar to MSTest Shows Partial Code Coverage on Compound Boolean Expressions.
What I suspect is happening is that you are not testing the early fail path when
i.Numberisnull.If you remember && will only evaluate the second expression if the first was true. In your case I suspect (as you haven’t mentioned how or what your tests are) that you do not have a test or scenario where there is an entry in your
allDealscollection wherei.Numberisnull.