I am working with a MongoDB database. I know when you insert a DateTime in Mongo, it will convert it to UTC. But I am doing a unit test, and my Assert is not working.
[TestMethod] public void MongoDateConversion() { DateTime beforeInsert = DateTime.Now; DateTime afterInsert; Car entity = new Car { Name = "Putt putt", LastTimestamp = beforeInsert };
I have to miss something obvious. When I look in the debugger, I see that the date and time match, but the statement still says no (but they do):
Result Message: Assert.AreEqual failed. Expected:<5/21/2015 8:27:04 PM>. Actual:<5/21/2015 8:27:04 PM>.
Any ideas what I'm doing wrong here?
EDIT:
I came up with two possible solutions, both of which require that I remember to do something (which is not always best to rely on ...):
One of them is to use the extension method to trim any DateTime coming out of the database:
public static DateTime Truncate(this DateTime dateTime) { var timeSpan = TimeSpan.FromMilliseconds(1); var ticks = -(dateTime.Ticks % timeSpan.Ticks); return dateTime.AddTicks(ticks); }
The other, after reading http://alexmg.com/datetime-precision-with-mongodb-and-the-c-driver/ , should mark any DateTime value in the POCO class:
public class Car : IEntity { public Guid Id { get; set; } [BsonDateTimeOptions(Representation = BsonType.Document)] public DateTime LastTimestamp { get; set; } }
source share