0

In my unit tests, using NUnit, I'm trying to compare the equality of two objects using this answer

public static void AreEqualByJson(object expected, object actual)
{
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var expectedJson = serializer.Serialize(expected);
    var actualJson = serializer.Serialize(actual);
    Assert.AreEqual(expectedJson, actualJson);
}

It works fine but for a property which is DateTime where it raises this error

String lengths are both 498. Strings differ at index 65.
Expected: "...onDateTime":"\\/Date(1410857388258)\\/","TransactionID":"Tra..."
But was:  "...onDateTime":"\\/Date(1410857388000)\\/","TransactionID":"Tra..."
---------------------------------------------^

The code that I'm using for that is this

[Test]
public void FromResponseToDecisionResponse_MapsAllProperties()
{
    //Arrange

    var now = DateTime.Now;
    var sourceResponse = CreateSourceResponse(now);
    var targetDecisionResponse = CreateTargetDecisionResponse(now);

    //Act
    var mappedResponse = _mapper.FromResponseToDecisionResponse(sourceResponse);

    //Assert 
    AreEqualByJson(targetDecisionResponse, mappedResponse);
}

Being

private static Response CreateSourceResponse(DateTime now)
{
    var sourceResponse = new Response
    {
        //More properties
        TransactionDateTime = now.ToString("yyyy-MM-ddThh:mm:sss")
    };
    return sourceResponse;
}

private static DecisionResponse CreateTargetDecisionResponse(DateTime now)
{
    var targetDecisionResponse = new DecisionResponse
    {
        //Other properties
        TransactionDateTime =  now,
        //More properties
    };
    return targetDecisionResponse;
}

Just before going into AreEqualByJson the dates are the same, but when they are serialized I got this offset that makes my test fail.

Is there something wrong or I should treat all dates as particular cases in AreEqualByJson (still maintaining it as a reusable method)?

Thanks,

UPDATE

As per Marc Gravell comment I ended up doing this in CreateTargetDecisionResponse

TransactionDateTime = DateTime.Parse(now.ToString("yyyy-MM-ddThh:mm:sss"))

Community
  • 1
  • 1
mitomed
  • 2,006
  • 2
  • 29
  • 58
  • 1
    They are different by 258ms; since you seem to be using `yyyy-MM-ddThh:mm:sss`, I'm not sure that this sub-second difference should be unexpected... – Marc Gravell Sep 16 '14 at 09:14
  • Thanks, @MarcGravell. You're completely right. I ended up doing some tricky thing (see update) but as it's just for a private method for this particular test I'm happy with it. – mitomed Sep 16 '14 at 09:18
  • It would be cleaner to compare strong-typed with a generic method. If you don't want to introduce dependencies on certain classes in the tests, you can serialize the objects (I'd suggest to use Json.Net for that as it's ~3 times as fast) and then deserialize them (with JavaScriptSerializer) into Dictionary. – bstenzel Sep 16 '14 at 09:35

1 Answers1

0

Object 1, TransactionDateTime = now.ToString("yyyy-MM-ddThh:mm:sss") (and assuming FromResponseToDecisionResponse is converting the string back to DateTime)

Object 2, TransactionDateTime = now, It can only be a coincidence if the two turn out to be same.. i.e. when now does not have any fractional seconds what so ever...

That's the most obvious incorrect part of the test.

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40