0

I have a method in an abstract class FakeEntityBaseRepository, called FindBy with the following code:

    public IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate, params string[] includeProperties)
    {
        Func<T, bool> func = predicate.Compile();
        Predicate<T> pred = func.Invoke;

        var result = list.FindAll(pred);

        if (result == null)
        {
            list.Clear();
            return list;
        }

        return result;
    }

I have several Unit Tests that are failing after updating the code above to what it is, an example test would be written as follows:

[Test]
        public void GetSequencedParts_WhenCalled_ReturnsAllSequencedParts()
        {
            // Arrange
            PartController partController = GetController(null, null, null, null);

            // Act
            OkObjectResult result = (OkObjectResult)partController.GetSequencedParts();

            // Assert
            Assert.IsInstanceOf<List<PartViewModel>>(result.Value);
            List<PartViewModel> PartList = (List<PartViewModel>)result.Value;
            Assert.AreEqual(1, PartList.Count);
        }

The Controller Method Looks like this:

    [HttpGet("sequenced")]
    public IActionResult GetSequencedParts()
    {
        try
        {
            IEnumerable<Part> parts = partRepository
                .FindBy(x => x.Sequences.Count > 0, "SomeString", "SomeString2")
                .ToList();

            IEnumerable<PartViewModel> partVMs = Mapper.Map<IEnumerable<Part>, IEnumerable<PartViewModel>>(parts);
            return new OkObjectResult(partVMs);
        }
        catch (Exception ex)
        {
            return new BadRequestObjectResult(ex);
        }
    }

I'm receiving an exception for this particular method. I should mention that the FindBy method works for other unit tests but is failing on these few.

the exception is a type of NullReferenceException and the following stack trace message is attached:

at lambda_method(Closure , Part )
   at System.Collections.Generic.List`1.FindAll(Predicate`1 match)
   at API.Tests.Fakes.FakeEntityBaseRepository`1.FindBy(Expression`1 predicate, String[] includeProperties) in C:\Jake_Projects\API\JakeUnitTestsRecovery\API.Tests\Fakes\FakeRepositories\FakeEntityBaseRepository.cs:line 82
   at API.Controllers.PartController.GetSequencedParts() in C:\Jake_Projects\API\JakeUnitTestsRecovery\API\Controllers\PartController.cs:line 68

Thank you for your time,

Jake

  • 1
    what is `partRepository` and where is `FindBy` defined? Seems it returns `null` for whatever reason. Just use your debugger. – HimBromBeere Aug 26 '19 at 15:26
  • 1
    @JakeOakley Maybe `x` is null at some point. Maybe `x.Sequences` is null at some point. That's the only lambda I see getting passed into FindBy(). Since we have no way of knowing what's in that enumeration, we can't answer the question for you. There's no general answer to "where's the null in my data". Since you know how to use a debugger, it'll be easy for you to find. Or query `parts`: `var nulls = parts.Where(x => x == null || x.Sequences == null).ToList();` – 15ee8f99-57ff-4f92-890c-b56153 Aug 26 '19 at 15:45
  • @HimBromBeere FindBy is defined at the top of the question, FWIW. – 15ee8f99-57ff-4f92-890c-b56153 Aug 26 '19 at 15:59

0 Answers0