[英]Code Coverage on Lambda Expressions
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. 我在整个代码中看到一个模式,其中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();
}
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?
那正确吗?
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. System.Linq.Enumerable
提供的所有LINQ扩展方法都表现出这种行为:即,lambda语句本身内的代码不会在您定义它的行上执行。 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。
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: 在进行单元测试时,如果您有一个方法返回您描述为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));
Each Assert of any of the dto's property will execute the lambda expression and then it will appear as covered. 任何dto属性的每个Assert都将执行lambda表达式,然后它将显示为覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.