String Date Compare
-
21-07-2021 - |
Question
In an BDD test I am comparing dates. When Comparing the dates are in strings. Even thought both the dates are same I get this message and the test fails
Expected object to be "01/20/2012 12:00:00 AM", but found "1/20/2012 12:00:00 AM".
One more thing is this happens just on my system. If I ask another developer to run the test, the test passes fine. Is there a setting of some type that I am missing?
The code part of it is
customer["DateOfBirth"].Should().Be(Helper.DateOfBirth.ToString());
Where customer is a hash table. this particular statement passes OK on other machines but not mine.
I know I can fix it by changing to date compare instead of String compare. But I was curious, since this is fine on other machines.
Solution
In an BDD test I am comparing dates. When Comparing the dates are in strings.
That sounds like your problem then. My guess is that you're comparing a date formatted in the default culture to one which has been hand-formatted in some other culture.
The solution is to perform the comparison with DateTime
values instead, rather than relying on formatted values. Allow the test runner to format both values if they're different.
OTHER TIPS
The dates are being formatted differently -- one has a leading 0.
You should either format them the same, or just compare the actual DateTime
values directly.