I get NullReferenceException when I try to test my class and I have no idea why? Exeption is connected to: o1.Products.Add. When I try to delete it, everything is ok.
public class Product
{
public string Name { get; set; }
public decimal Value { get; set; }
}
public class Order
{
public List<Product> Products { get; set; }
private decimal value { get; set; }
public decimal Value
{
get { return value; }
set
{
if (Products.Count == 0)
value = 0;
else
value = Products.Sum(x => Convert.ToInt32(x));
}
}
}
[TestClass]
public class Tests
{
[TestMethod]
public void OrderTest()
{
//Arrange
Product p1 = new Product();
Order o1 = new Order();
//Act
p1.Value = 10;
o1.Products.Add(p1);
//Assert
Assert.AreEqual(30, o1.Value);
}
}