0

I'm trying to write a unit test that calculates the total of an Order. However, there is another method that is called from the CalculateOrder() method that I'm testing and my test keeps failing saying "Object reference not set to an instance of an object."

This is the CalculateOrder method I'm testing...

public App_OrderForm CalculateOrder(App_OrderForm form)
{
    try
    {
        // Get the possible fees for the form group
        short? orderGroupId = form.Ref_OrderType == null
                                 ? GetOrderGroup(form.OrderTypeId)
                                 : form.Ref_OrderType.orderGroupId;

        Dictionary<Ref_OrderType, List<short>> fees = 
                           GetFeeTypeForOrderGroup((OrderGroup)orderGroupId);

It is the last line of code there that is creating the error.

This is my unit test...

// Arrange
_mockUnitOfWork = new Mock<IUnitOfWork>();
_feeCalculator = new FeeCalculator(_mockUnitOfWork.Object);
App_OrderForm form = GenerateMockedForm(OrderFormType.Wholesaler);
form.Ref_OrderFormType = new Ref_OrderFormType
{
     OrderFormTypeId = 1,
     OrderGroupId = 1
};

foreach (var item in form.App_OrderFormItem)
{
    // add calculated fees
    item.App_OrderFormItemCalculatedFee = new List<App_OrderFormItemCalculatedFee>
    {
        new App_OrderFormItemCalculatedFee()
        {
            OrderFormItemCalculatedFeeId = 1,
            FeeTypeId = 1,
            Amount = 0.044M,
            NumberOfFees = 1
        },
    };
}

// Act
Mock<IReferenceDataService> rds = new Mock<IReferenceDataService>();
rds.Setup(x => x.GetAllPayableStatus())
  .Returns(new List<Ref_Status> {new Ref_Status() {StatusId = (short) Status.ReadyForPayment}});
_feeCalculator.CalculateForm(form, false);

Am I supposed to setup the method GetFeeTypeForOrderGroup()? At the start of my unit test I've set up an Order Form and given the OrderGroupId a value so why doesn't the method GetFeeTypeForOrderGroup() not get run or why is it returning the error?

Hope that made sense,

Thanks

dataCruncher02
  • 361
  • 2
  • 5
  • 13
  • I suspect `GetFeeTypeForOrderGroup` itself *is* being run, but is immediately throwing the exception. It's hard to tell with an incomplete example. Have you stepped through the code in a debugger? – Jon Skeet Aug 17 '18 at 10:38
  • We'd need to see `GetFeeTypeForOrderGroup` and the stack trace should tell you where in that method the null reference occurs. – juharr Aug 17 '18 at 10:38
  • 1
    [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) will help you. The first step is to find out what variable is null. – stuartd Aug 17 '18 at 10:51

0 Answers0