unit test case returning null while unit testing

Viewed 47

I have a below method which simply unnest the list within list and and create merged record all sub lists

e.g.

testResultDTO similar to

{
dept_name :'',
dept_id:'',
personList :'<List>'

}

personList is list similat to

 {
first_name :'',
Last_name:'',
workHistList:'<List>'

    }

workHistList is similar to

{
task_name:'',
task_description:''}

 `

And the method is below

   public async Task<FileDTO> GetWorkList(InputParameter inputParameter)
    {
      TestResultDTO testResultDTO = await GetPersonWorkList(inputParameter);

      var testDto = new List<TestDto>();

      if (testResultDTO != null)
      {     
        testDto = personWorkFile(testResultDTO);
      }
       
      return testDto;
}

The problem is in method personWorkFile

private List<testDTO> personWorkFile(testResultDTO testResultDTO)
  {
  List<testDTO> testDTO = new List<testDTO>();

  if (testResultDTO.workList is null) return new();

  foreach (var testItem in testResultDTO.workList)
  {
    if (testItem.personList != null && testItem.personList.Any())
    {
      for (var j = 0; j < testItem.personList.Count; j++)
      {
        List<testDTO> testDTOTemp = new List<testDTO>();
        for (var i = 0; i < testItem.personList[j].workHistList.Count; i++)
        {
          testDTOTemp.Insert(i, mapper.Map(testItem.personList[j].workHistList[i], mapper.Map<testDTO>(testItem.personList[j])));
        }

        testDTO.AddRange(testDTOTemp);
      }
    }
  }
  return testDTO;
}
`

what it does is it create a finale list with record similar to

{dept_name,dept_id,first_name, last_name, task_name, task_description}

Now when i create a unit test for it, it always fail because, it always insert null in list testDTOTemp.insert

my content of test case code is below

  mapperMock.Setup(m => m.Map<List<TestResultDTO>, List<TestDTO>>(testResultDTO)).Returns(testDTO);
  
  //call actual service
  var result = await workService.GetWorkList(inputParameter);

Can someone please help me understand why it is not able to insert any data testDTOTemp list

1 Answers

The mock replays the setup by matching the method arguments. In this particular case, it will try to match the method call with the specified instance of TestResultDTO.

Override the Equals() method in TestResultDTO to let the mock compare the argument in order to return as per the setup.

Ex:

public class TestResultDTO
{
  public int Id {get; set;}
}

var dto01 = new TestResultDTO(){Id = 1};
var dto02 = new TestResultDTO(){Id = 1};

dto01.Equals(dto02); // This will result in false in spite of having same Ids 

If the above class is modified to

public class TestResultDTO
{
  public int Id {get; set;}
  public override bool Equals(object other)
  {
    var right = other as TestResultDTO;
    if(right == null) return false;
    var left = this;
    var result = left.Id == right.Id;
    return result;
  }
}

var dto01 = new TestResultDTO(){Id = 1};
var dto02 = new TestResultDTO(){Id = 1};

dto01.Equals(dto02); // This will result in true
Related