MbUnit Assert.AreEqual DateTime 和 Decimal 不等于

发布于 2024-10-10 15:06:07 字数 1016 浏览 2 评论 0原文

我有一个像这样的类结构

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; 
}

,我填充所有属性并将其保存到数据库,之后我从数据库获取该对象

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

并尝试以这种方式比较值

Assert.AreEqual(entity, entityFromDb);

上比较失败

,但在日期时间和十进制值预期值 平等。

期望值: ID = 28, 创建时间 = 2011-01-05T14:06:32。6874218Z, 删除于 = null, 描述=“描述”, 持续时间=2000m, 标签=“测试实体”, 修改时间 = 2011-01-05T14:06:32.6874218Z

实际值: ID = 28, 创建时间 = 2011-01-05T14:06:32。0000000, 删除于 = null, 描述=“描述”, 持续时间 = 2000.00000m, 标签=“测试实体”, ModifiedAt = 2011-01-05T14:06:32.0000000

我可以以某种方式比较这些值,但不比较每个字段

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.0000000,
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-10-17 15:06:07

一种选择是更改 DateTimes 的设置器,以便它们在该点执行截断 - 基本上问题是您的数据库没有存储亚秒精度,因此在实体中也不包含它似乎是合理的。

我希望十进制值已经被比较为相等,即使它们具有不同的存储精度。 (1.000m 和 1.0m 有不同的表示方法,但被视为相等。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文