如何在某些 where 条件下模拟哪个返回整数列表?

How to mock which is returning list of integers on some where condition?

我在下面的步骤中阻止了

var ids = _repository.GetIQueryable<Customers>().Where(lrt => lrt.IsActive == true &&
                       lrt.NextRoleId == defaultRoleSetting.RoleId &&
                       lrt.NextUserId == null).Select(x => x.MasterId).Distinct().Take(100).ToHashSet();

我试过了,但找不到正确的 Returns 语法

_mockRepository.Setup(s => s.GetIQueryable<Customers>()).Returns<List<int>>(ids =>
{
  return ????;
});

当您为 GetIQueryable<Customers> 创建设置时,您不会 return 整数列表,而是 IQueryable 之后过滤的 Customers 个对象:

IQueryable<Customers> models = new Customers[]  {
    new Customers() { MasterId = 1, IsActive = true, NextRoleId = nextRoleId, ... }, 
    new Customers() { MasterId = 2, IsActive = false, NextRoleId = nextRoleId, ... }, 
    new Customers() { MasterId = 3, IsActive = true, NextRoleId = nextRoleId, ... }, 
  }).AsQueryable();

_mockRepository
    .Setup(s => s.GetIQueryable<Customers>())
    .Returns(models);

在此示例中,您创建了一个包含 Customers 个对象的数组并设置了客户的属性,以便过滤器随后对 IQueryable 起作用。在 Customers 对象上设置哪些属性取决于 类 和您的测试用例。

通过下面的代码解决了我的问题,谢谢大家。

var fakeCustomers = FakeCustomers();
_repository.Setup(s => s.GetIQueryable<Customers()).Returns(fakeCustomers.AsQueryable());
    
    private List<Customers> FakeCustomers()
            {
                string fakeData = @"[{
      'Id': '118',
      'CreatedBy':'901358ab-32e3-4a52-9182-ba68a0a03b50',
      'SId':'4',
      'UserId':'61b202da-0120-4f3c-8d29-829a81576cd1',
      'NId':'7'
    }]";
                return JsonConvert.DeserializeObject<List<FccCustomers>>(fakeData);
            }