0

The Method should only remove from my DataStructures everything from BoughtItems List and for some reason everytime i try to unit test the method it throws a NullReferenceException (in the actual program it works fine)

internal static bool DealAccepted(int amount)
        {
            foreach (var item in Manager.BoughtItems)
            {
                //Reduces Amount from Item and resets its LastTimeSold Property
                if (item.Amount > amount)
                {
                    item.Amount -= amount;
                    AddToTimeCheckListManager.AddToTimeCheckListBuy(item);

                    if (item.Amount < Manager.MinSupplyAmount)
                    {
                        return false;
                    }
                }
                //if we tried to order more or equal to the amount of current product
                else
                {
                    amount -= item.Amount;
                    TryRemoveItemFromDataStructures(item);
                }
            }
            DeleteAllLists();
            return true;
        }

This is the unit test

[TestMethod]
public void BuyBox_BuyEntireAmountOfSingleProduct_ProductDeleted()
{
    Manager manager = new Manager();
    Box box = new Box(6, 6);
    manager.AddItem(box, 10);

    ValueData data = new ValueData(box);
    Manager.BoughtItems.Add(data);
    manager.DealAccepted(5);
}
  • 1
    You're calling a static method on an instance which shouldn't be possible. And in your static method it's iterating over `Manager` which is likely not the `manager` you have in your test. – juharr Jun 03 '22 at 12:59
  • 1
    (1) This isn't the real code. (2) There's no indication in your post of which line is throwing. – Matthew Watson Jun 03 '22 at 13:00
  • There’s no indication beach it’s a unit test method that’s why I Added the Mathis that throws the exception @MatthewWatson – Shalev Yegudayev Jun 03 '22 at 15:30

0 Answers0