Reputation: 515
I am doing a unit test for datetime and it fails because Test Name:
GetDateTime2 Result Message: Assert.AreEqual failed. Expected:<28/05/2017 20:00:00 (System.String)>. Actual:<28/05/2017 20:00:00 (System.DateTime)>.
Is there any way of comparing the string against the datetime of do i have to change the properties of string date and string time?
public void GetDateTime()
{
FootballEvent football = new FootballEvent();
football.Time = "20:00:00";
football.Date = "28/05/2017";
var footballtime = football.GetDateTime();
var expected = "28/05/2017 20:00:00";
Assert.AreEqual(expected, footballtime);
}
Upvotes: 5
Views: 3791
Reputation: 56423
as some people have stated within the comments why not just make a DateTime
object then compare them?
Example:
var footballtime = football.GetDateTime();
var expected = "28/05/2017 20:00:00";
DateTime expectedDate = DateTime.Parse(expected);
Assert.AreEqual(expectedDate, footballtime);
Upvotes: 5
Reputation: 10818
You could call ToString()
with whatever format you want your time to be in for comparison.
var expected = "28/05/2017 20:00:00";
//Use HH for 00-23, use H for 0-23, use hh for 01-12 and use h for 1-12
Assert.AreEqual(expected, footballtime.ToString("dd/MM/yyyy HH:mm:ss"));
However it seems like you should be comparing both in the proper format (DateTime).
Ask yourself why you are initializing the times via string (football.Time = "20:00:00";
), why not use the proper data type for Dates and Times (DateTime
)
Upvotes: 1