Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2012 code coverage partially touched area

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))
like image 538
Oleksandr Kobylianskyi Avatar asked Dec 14 '25 17:12

Oleksandr Kobylianskyi


1 Answers

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.Number is null.

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 allDeals collection where i.Number is null.

like image 82
Shaun Wilde Avatar answered Dec 18 '25 09:12

Shaun Wilde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!