代码覆盖的Lambda表达式 [英] Code Coverage on Lambda Expressions

查看:130
本文介绍了代码覆盖的Lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在我的代码,其中lambda表达式显示为未覆盖的代码覆盖的模式,调试器单步执行代码,也没有条件块。

I'm seeing a pattern throughout my code where the lambda expression is showing as not covered in code coverage, the debugger DOES step through the code and there are no conditional blocks.

public CollectionModel()
{
    List<Language> languages = LanguageService.GetLanguages();
    this.LanguageListItems =
        languages.Select(
            s =>
            new SelectListItem { Text = s.Name, Value = s.LanguageCode, Selected = false }). // <-- this shows as not covered
            AsEnumerable();
}



这是有点古怪。任何想法

It is somewhat odd. Any ideas?

推荐答案

我想你的意思是,调试器不跨过指定的行?;是这样吗?

What I think you mean is that the debugger is not stepping over the indicated line; is that right?

如果这是你的问题,那么答案是,至少在这种特殊情况下,你看到的是的延迟执行。所有由 系统提供的LINQ扩展方法.Linq.Enumerable 出现此行为:即拉姆达语句本身内部的代码的的你在哪里被定义它的行执行。一旦生成的对象是列举了只执行的代码

If that's your question, then the answer is that, at least in this particular case, what you are seeing is deferred execution. All of the LINQ extension methods provided by System.Linq.Enumerable exhibit this behavior: namely, the code inside the lambda statement itself is not executed on the line where you are defining it. The code is only executed once the resulting object is enumerated over.

将此你已经发布的代码下面:

Add this beneath the code you have posted:

foreach (var x in this.LanguageListItems)
{
    var local = x;
}



在这里,你会看到调试器跳回到你的λ

Here, you will see the debugger jump back to your lambda.

这篇关于代码覆盖的Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆