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