I'm new here and new to C# also. I'm trying to learn OOP with C# and I'm currently doing the Pluralsight course: Object-Oriented Programming Fundamentals in C#. I'm getting on well with it, but I've been completely stuck on an error for the last couple of days that I keep getting when running two of the unit tests in my project (the rest of the tests have passed ok).
Basically the course walks you through building an Ordering System which Customers can use to order different products. The unit test which produces the error is the "CustomerRepositoryTest" which has two methods: one for testing to make sure the Customer Repository returns a Customer when a specific id is passed in and one that checks whether the Customer returns with a list of addresses.
The values are hard coded into the Customer and Customer Repository classes so that there is no need to set up a database for the purpose of the course.
The error I keep getting is this:
ACMBL.TEST.CustomerRepositoryTest.RetrieveExistingWithAddress threw Exception. System.NullReferenceException: Object not set to instance of an object
I've checked through the completed course files but my code seems to be the same as them, so I'm a bit lost as to why the tests keep failing. Here's the code for the CustomerRepositoryTest file:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ACM___Business_Layer;
using System.Collections.Generic;
namespace ACMBL.TEST
{
[TestClass]
public class CustomerRepositoryTest
{
[TestMethod]
public void RetrieveExisting()
{
//-- Arrange
var customerRepository = new Customer_Repository();
var expected = new Customer(1)
{
EmailAddress = "[email protected]",
FirstName = "Frodo",
LastName = "Baggins"
};
//-- Act
var actual = customerRepository.Retrieve(1);
//-- Assert
//Assert.AreEqual(expected, actual);
Assert.AreEqual(expected.CustomerId, actual.CustomerId);
Assert.AreEqual(expected.EmailAddress, actual.EmailAddress);
Assert.AreEqual(expected.FirstName, actual.FirstName);
Assert.AreEqual(expected.LastName, actual.LastName);
}
[TestMethod]
public void RetrieveExistingWithAddress()
{
//-- Arrange
var customerRepository = new Customer_Repository();
var expected = new Customer(1)
{
EmailAddress = "[email protected]",
FirstName = "Frodo",
LastName = "Baggins",
AddressList = new List<Address>()
{
new Address()
{
AddressType = 1,
StreetLine1 = "Bag End",
StreetLine2 = "Bagshot row",
City = "Hobbiton",
State = "Shire",
Country = "Middle Earth",
PostalCode = "144"
},
new Address()
{
AddressType = 2,
StreetLine1 = "Green Dragon",
City = "Bywater",
State = "Shire",
Country = "Middle Earth",
PostalCode = "146"
}
}
};
//-- Act
var actual = customerRepository.Retrieve(1);
//-- Assert
Assert.AreEqual(expected.CustomerId, actual.CustomerId);
Assert.AreEqual(expected.EmailAddress, actual.EmailAddress);
Assert.AreEqual(expected.FirstName, actual.FirstName);
Assert.AreEqual(expected.LastName, actual.LastName);
for (int i = 0; i < 1; i++)
{
Assert.AreEqual(expected.AddressList[i].AddressType, actual.AddressList[i].AddressType);
Assert.AreEqual(expected.AddressList[i].StreetLine1, actual.AddressList[i].StreetLine1);
Assert.AreEqual(expected.AddressList[i].City, actual.AddressList[i].City);
Assert.AreEqual(expected.AddressList[i].State, actual.AddressList[i].State);
Assert.AreEqual(expected.AddressList[i].Country, actual.AddressList[i].Country);
Assert.AreEqual(expected.AddressList[i].PostalCode, actual.AddressList[i].PostalCode);
}
}
}
}
Any help on this would be much appreciated. Let me know if you need any further information, something tells me I haven't explained it enough lol.
Thanks.

New Topic/Question
Reply


MultiQuote



|