Lambda 表达式的代码覆盖率

发布于 2024-09-19 22:05:50 字数 446 浏览 6 评论 0原文

我在整个代码中看到一种模式,其中 lambda 表达式显示为未包含在代码覆盖范围内,调试器确实单步执行代码并且没有条件块。

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();
}

这有点奇怪。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在你怀里撒娇 2024-09-26 22:06:10

当您进行单元测试时,如果您有一个方法返回您描述为 LanguageListItems 的列表,您可以在单元测试中执行此操作:

var result = await controller.SomeAction();
var okObjectResult = Assert.IsType<OkObjectResult>(result);
var results = Assert.IsAssignableFrom<IEnumerable<YourDtoClass>>okObjectResult.Value);
Assert.NotNull(results);
Assert.All(results, dto => Assert.NotNull(dto.PendingItemCount));
Assert.All(results, dto => Assert.NotNull(dto.ApprovedItemCount));

任何 dto 属性的每个断言都将执行 lambda 表达式,然后它将显示为覆盖。

When you are making the unit tests, if you have a method that returns the list you described as LanguageListItems, you can do this in the unit test:

var result = await controller.SomeAction();
var okObjectResult = Assert.IsType<OkObjectResult>(result);
var results = Assert.IsAssignableFrom<IEnumerable<YourDtoClass>>okObjectResult.Value);
Assert.NotNull(results);
Assert.All(results, dto => Assert.NotNull(dto.PendingItemCount));
Assert.All(results, dto => Assert.NotNull(dto.ApprovedItemCount));

Each Assert of any of the dto's property will execute the lambda expression and then it will appear as covered.

绅刃 2024-09-26 22:06:07

我认为你的意思是调试器没有跨过指定的行;对吗?

如果这是您的问题,那么答案是,至少在这种特殊情况下,您看到的是延迟执行System.Linq 提供的所有 LINQ 扩展方法.Enumerable 表现出这种行为:即,lambda 语句本身内的代码不会在您定义它的行上执行。仅在枚举结果对象后才会执行该代码。

将其添加到您发布的代码下方:

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

在这里,您将看到调试器跳回您的 lambda。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文