Code line in Unit Test that is not accepted by code coverage

Asked

Viewed 110 times

1

I have a test routine where a certain line of code is not accepted by the coverage.

Imagem da rotina com a linha de código que não é aceita

This is a test to return Defaultvalue since the object calling it is null.

This is the method being tested

public static TResult IfNotNull<TObject, TResult>(this TObject obj, 
    Func<TObject, TResult> action, TResult defaultValue)
{
    if (action == null)
        throw new ArgumentNullException(nameof(action));

    return obj == null ? defaultValue : action(obj);
}

See that it is marking as 100% tested.

Rotina sendo testada que está marcando 100%

How to solve this problem?

1 answer

0

I don’t know what benefit you’re looking for, being covered in tests in general (ShouldReturnDefaultValueSinceObjectIsNull). Surely your IDE must be including the tests in the coverage. Coverage should be used as a tool where you can view areas of code that are or are not covered by testing. Note that even if the coverage shows that the lines are covered, it does not mean that the desired behavior of the function is being tested. Looking at this function/method I see 3 behaviors/functionalities that must be tested, if these are the requirements.

  1. when the action is null then throw new ArgumentNullException(nameof(action));
  2. when the obj is null then return 'defaultValue';
  3. when the obj is not null then return 'action(obj)';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.