So basically I am trying to run a unit test on one of the classes in my program, the method in the class works fine but for some reason I can't get the unit test to pass.
Here is my class:
public class Driver
{
public DateTime Date { get; set; }
public String Start { get; set; }
public String End { get; set; }
public int Distance { get; set; }
public Driver(DateTime date, String start, String end, int distance)
{
this.Date = date;
this.Start = start;
this.End = end;
this.Distance = distance;
}
public override String ToString()
{
return string.Format("Date: {0}, Start Postcode: {1}, End Postcode: {2}, Distance: {3}miles", this.Date.ToString("dd/MM/yyyy"), this.Start, this.End, this.Distance);
}
}
} `
Here is my test class:
[TestClass]
public class Driverdetailstest
{
private Driver TestDriver;
[TestMethod]
public void DriverDataTest()
{
DateTime date = new DateTime(2017, 12, 15);
string start = ("SR47BB");
string end = ("SR47FY");
int distance = 10;
var expected = string.Format("Date: {0}, Start Postcode: {1}, End Postcode: {2}, Distance: {3}miles", date.ToString("dd/MM/yyyy"), start, end, distance);
TestDriver = new Driver(date, start, end, distance);
Convert.ToString(TestDriver);
Assert.AreEqual(expected, TestDriver);
}
}
Despite the strings coming back the same I get this error as the reason the test is failing.
Message: Assert.AreEqual failed. Expected:<Date: 15/12/2017, Start Postcode: SR47BB, End Postcode: SR47FY, Distance: 10miles (System.String)>. Actual:<Date: 15/12/2017, Start Postcode: SR47BB, End Postcode: SR47FY, Distance: 10miles (TaxiTracker.Driver)>.
What is it I am doing wrong?
Thanks
Convert.ToString(TestDriver);but didn't assign it to a variable, just doAssert.AreEqual(expected, TestDriver.ToString());.