验证 linq select 表达式中方法调用的数量

Verifying number of method calls within a linq select expression

我的单元测试正在验证调用模拟方法的次数:

测试设置

var mockedDependency= new Mock<IDependeny>();
mockedDependency.Setup(x => x.CheckSomething(It.IsAny<string>())).Returns(true);

//make sure CheckSomething is called x times
                
//verify 
mockedDependency.Verify(x => x.CheckSomething(It.IsAny<string>()), Times.Exactly(xtimes));

调用 CheckSomething

有效(显示正确的 x 调用次数):

//var itemsToCheck  = some array
CheckResults[] checks = new CheckResults[knownLegth];
for (int i = 0; i < checks.Length; i++)
{
   checks[i] = new CheckResults
   {
       Name = itemsToCheck[i],
       Valid = dependency.CheckSomething(itemsToCheck [i])
   };
}

但这不起作用(显示对 CheckSomething 的零调用):

var checks = itemsToCheck .Select(x => new CheckResults
{
    Name = x,
    Valid = dependency.CheckSomething(x)
});

对于这两种方法,CheckSomething 都被正确模拟,返回 true。 最小起订量的验证在 Select linq 表达式中不起作用是否有特定原因?
使用最小起订量 v4.15.1

解决方案 根据@Vernou 的评论,直接在测试方法中调用 ToList()。

如何解决我的问题。

由于我正在测试返回 ActionResult 的控制器,为了防止在控制器中更改代码,请在单元测试中调用 ToList():

[Fact]
public void My_Unit_Test()
{ 
    int checks = 10; 
    string[] items= new string[checks]; 
    var mockedDependency= new Mock<IDependeny>();
    mockedDependency.Setup(x => x.CheckSomething(It.IsAny<string>()))
       .Returns(true);

     var myController= new MyController(mockedDependency.Object);
     var actionResult = myController.CallMethod(items);
     
     var actionResult = dailyStockPricesController.Verify(fileNames);

     var okResult = actionResult.Result as OkObjectResult; 
     Assert.NotNull(okResult);

     var value = (IEnumerable<FileUploadVerification>)okResult.Value;

     Assert.Equal(10, value.ToList().Count); //call ToList() at least once
     mockedDependency.Verify(x => x.CheckSomething(It.IsAny<string>()), Times.Exactly(checks));
 }