Weird test coverage results for iterator block, why are these statements not executed?

后端 未结 2 1099
死守一世寂寞
死守一世寂寞 2021-02-07 17:43

I\'m using dotCover to analyze code coverage of my unit tests, and I\'m getting some strange results... I have an iterator method for which the coverage is not complete, but the

2条回答
  •  面向向阳花
    2021-02-07 18:13

    In addition to your question and the in detail answer, I had the following behaviour.

        // less than 100% coverage
        public static IEnumerable ForEachYieldDo(this IEnumerable source, Action action)
        {
            foreach (var x in source)
            {
                action(x);
                yield return x;
            }
        }
    
        // 100% code coverage
        public static IEnumerable ForEachSelectDo(this IEnumerable source, Action action)
        {
            return source.Select(x =>
            {
                action(x);
                return x;
            });
        }
    

    Both functions have the same behaviour. The action is only executed, if the item is processed. If the retrieval of the items is stopped, the action is not executed.

提交回复
热议问题