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

I blocked in below step

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

I tried this but I don't find the right Returns syntax

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

2 answers

  • answered 2022-04-20 06:03 Markus

    As you are creating a setup for GetIQueryable<Customers> you'd not return a list of integers, but instead an IQueryable of Customers objects that are filtered afterwards:

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

    In this sample, you create an array of Customers objects and set the properties of the customers so that the filter afterwards works on the IQueryable. Which properties to set on the Customers objects depends on the classes and your test case.

  • answered 2022-04-21 04:14 abbas ahmed

    by below code my issue solved thanks every one.

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

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum