0

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.

katie77
  • 1,797
  • 4
  • 25
  • 43
  • 2
    I imagine the problem is with your code. I *have* to imagine, as you have not actually shown us any. – D'Arcy Rittich Oct 30 '12 at 14:46
  • if the problem is with code, it should not be running fine on another machine correct? – katie77 Oct 30 '12 at 14:47
  • If you are comparing your dates as strings, then the dates you listed as strings, are not the same. You should convert to DateTimes then compare. – DougEC Oct 30 '12 at 14:49
  • 2
    @katie77: No, that's not a valid assumption. Your code is probably culture-specific - so will work on computers using one culture by default, but not on others. – Jon Skeet Oct 30 '12 at 14:51

2 Answers2

6

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

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.

Jon B
  • 51,025
  • 31
  • 133
  • 161
  • @katie77 it can be, depending on how the date is turned into a string. The best way to compare dates is to use the `DateTime` type. – Jon B Oct 30 '12 at 14:53