开发者

MbUnit Assert.AreEqual DateTime and Decimal are not equals

I have a class structure like this

public class Entity
{
    public int Id { get; set; }
    public string Label { get; set; }
    public string Description { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime ModifiedAt { get; set; }
    public DateTime? DeletedAt { get; set; 
}

I fill all the properties and save it to the DB, after that I get this object from the DB

var entity = CreateEntity()
SaveToDB(entity);
var entityFromDb = GetFromDB();

and try to compare the values in this way

Assert.AreEqual(entity, entityFromDb);

and the comparison is faild on the DateTime and Decimal values

Expected values to be equal.

Expected Value : Id = 28, CreatedAt = 2011-01-05T14:06:32.6874218Z, DeletedAt = null, Description = "Description", Duration = 2000m, Label = "Test Entity", ModifiedAt = 2011-01-05T14:06:32.6874218Z

Actual Value : Id = 28, CreatedAt = 2011-01-05T14:06:32.0开发者_运维技巧000000, DeletedAt = null, Description = "Description", Duration = 2000.00000m, Label = "Test Entity", ModifiedAt = 2011-01-05T14:06:32.0000000

Can I compare this values in some way, but without make a comparison of each fields


One option would be to change your setters for DateTimes so that they perform the truncation at that point - basically the problem is that your DB isn't storing subsecond precision, so it seems reasonable not to have it in the entity either.

I'd expect the decimal values to be compared as equal already, even though they have different stored precision. (1.000m and 1.0m have different representations, but are deemed equal.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜