1

I have been getting a null reference error when running my test but cant figure it out. Below is my test

[Test]
[TestCase(...)]
public void Get_ShouldReturnTradesMock(string field, Operator op, int dayManip, SortDirection sortDir, string filterTerm, int pageIndex, int pageSize, int expectedRecordMinSize)
{
    using (var _imprintDbContext = new ImprintDbContext(_dbContextOptions))
    {
        var mockExecRepo = new Mock<IExecutionReportRepository>();
        mockExecRepo.Setup(mock => mock.GetFilteredTrades(It.IsAny<IEnumerable<Query>>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>())).Verifiable();//.Returns<Task<PagedResult<ExecutionReport>>>(x => null);

        var uow = new Mock<IUnitOfWork>();
        uow.SetupGet(x => x.ExecutionReports).Returns(mockExecRepo.Object);

        var controller = new TradesController(uow.Object);

        var query = new Query()
        {
            Field = field,
            Operator = op,
            Search = DateTime.Now.Add(TimeSpan.FromDays(dayManip)).Date.ToString("yyyy-MM-dd"),
            SortDirection = sortDir
        };

        TradesController.TradesBody tb = new TradesController.TradesBody()
        {
            queries = new[] { query },
            filterTerm = filterTerm
        };

        var results = controller.Get(tb, pageIndex, pageSize);

        uow.Verify(mock => mock.ExecutionReports.GetFilteredTrades(new[] { query }, pageIndex, pageSize, filterTerm), Times.Once());
    }
}

And the definitions of some of the objects I am mocking:

public interface IExecutionReportRepository : IRepository<ExecutionReport>
{
    ...
    Task<IPagedResult<ExecutionReport>> GetFilteredTrades(IEnumerable<Query> queries, int pageIndex, int pageSize, string filterTerm);

}

UnitOfWork:

public class UnitOfWork : IUnitOfWork
{
    private readonly DbContext _context;

    public UnitOfWork(DbContext context, IExecutionReportRepository executionReportRepository)
    {
        _context = context;
        ExecutionReports = executionReportRepository;
    }

    public IExecutionReportRepository ExecutionReports { get; }
}

TradesController:

public class TradesController : Controller
{
    public class TradesBody
    {
        public IEnumerable<Query> queries;
        public string filterTerm;
    }

    private readonly IUnitOfWork unitOfWork;

    public TradesController(IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }

    /// <summary>
    /// Gets a list of trades for the current date
    /// </summary>
    [HttpPost]
    public async Task<IActionResult> Get([FromBody] TradesBody postBody, int pageIndex = 0, int pageSize = 100)
    {
            string filterTerm = postBody.filterTerm ?? "";
            IEnumerable<Query> queries = postBody.queries;
            IPagedResult<Domain.Entities.Core.ExecutionReport> queryResult;
            queryResult = await unitOfWork.ExecutionReports.GetFilteredTrades(queries, pageIndex, pageSize, filterTerm); //Null reference error here
            return Ok(queryResult);
    }
}

When stepping through the code, I do not see any null objects and thus cannot actually see/understand where the null reference is actually being found, however I have noticed that I cannot see the method definition during debug time for 'GetFilteredTrades. Judging by this, my mocked method is not connecting to the method being executed, however I only have one GetFilteredTrades.

How do I resolve the null reference error being thrown in the TradesController and successfully run my test?

Luke
  • 776
  • 9
  • 24
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Zer0 Mar 18 '19 at 14:18
  • You are not setting up `GetFilteredTrades` to return anything so it is failing when you try to await it. – Nkosi Mar 18 '19 at 14:27

1 Answers1

1

You are not setting up GetFilteredTrades to return anything so it is failing when you try to await it.

mockExecRepo
    .Setup(mock => mock.GetFilteredTrades(It.IsAny<IEnumerable<Query>>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>()))
    .ReturnAsync(Mock.Of<IPagedResult<ExecutionReport>>()) //<--THIS
    .Verifiable();

Also the method under test is async, so the test should be async as well.

[Test]
[TestCase(...)]
public async Task Get_ShouldReturnTradesMock(string field, Operator op, int dayManip, SortDirection sortDir, string filterTerm, int pageIndex, int pageSize, int expectedRecordMinSize)
{

and the method under test call awaited

var results = await controller.Get(tb, pageIndex, pageSize);

Finally, you are verifying the wrong mock based on the setup. Since the mockExecRepo setup has Verifiable() then you can simply call Verify() on the mock.

//...

//Act
var results = await controller.Get(tb, pageIndex, pageSize);

//Assert
mockExecRepo.Verify();

to verify that it was invoked as expected.

Nkosi
  • 235,767
  • 35
  • 427
  • 472