如何验证是否已从数据库正确获取集合?

发布于 2024-11-18 16:16:42 字数 888 浏览 2 评论 0原文

我正在尝试为我的存储库编写集成测试,但我找不到一种很好的方法来断言结果就是我想要的结果。

我有一个类,其中包含所有测试数据的静态属性 - 项目、用户等。在我的测试设置中,我使用 NHibernate 将所有这些实体插入到我的数据库中(并且我已经能够验证这是否有效 - 记录设置完成后它们应该在那里)。

我的存储库(稍微简化)是这样做的:

public IEnumerable<MyEntity> GetEntitiesByProjectID(id)
{
    return session.Linq<MyEntity>().Where(e => e.Project.ID == id).ToList();
}

在我的测试中,我尝试断言结果是正确的,如下面的代码示例所示,但由于多种原因,集合并不等效 - 主要的一个是引用的其他实体被替换为castle 代理(Castle.Proxies.UserProxy 而不是 User)。

Assert.AreElementsEqualIgnoreOrder(
    Data.MyEntites.Where(e => e.Project.ID == 1), results)

还有一些其他奇怪的地方 - 例如日期时间的表示方式。在我的 .NET 类型中,日期时间字符串表示形式为 2011-07-05T14:28:11.5655935+02:00,而来自数据库的日期时间字符串表示形式为 2011-07-05T14:28 :11.0000000,即没有时区指示器。

通过检查,我可以验证存储库是否确实获取了正确的元素,因此我的测试显然是在测试错误的东西。

我在这里做错了什么?

I am trying to write an integration test for my repository, but I can't find a nice way to assert that the results are what I want them to.

I have a class with static properties for all my test data - Projects, Users etc. In the setup of my test, I insert all these entities in my database using NHibernate (and I've been able to verify that this works - the records are there as they should after the setup is complete).

My repository (slightly simplified) does this:

public IEnumerable<MyEntity> GetEntitiesByProjectID(id)
{
    return session.Linq<MyEntity>().Where(e => e.Project.ID == id).ToList();
}

In my test, I try to assert that the results are correct as in the code example below, but the collections are not equivalent for several reasons - the primary one being that referenced other entites are replaced with castle proxies (Castle.Proxies.UserProxy instead of User).

Assert.AreElementsEqualIgnoreOrder(
    Data.MyEntites.Where(e => e.Project.ID == 1), results)

There are some other oddities too - for example the way datetimes are represented. In my .NET types, the datetime string representations are 2011-07-05T14:28:11.5655935+02:00, while the ones coming from the db are 2011-07-05T14:28:11.0000000, i.e. without the timezone indicator.

By inspection I can verify that the repository has really fetched the right elements, so my test is obviously testing the wrong thing.

What am I doing wrong here?

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

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

发布评论

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

评论(1

失眠症患者 2024-11-25 16:16:43

您确定必须测试数据层吗?我相信 NHIbernate 经过了充分的测试,您必须测试您的服务层并为数据提供模拟。

如果不是这种情况,请在集成测试期间将延迟加载设置为 false,它应该有助于解决 UserProxy 而不是 User 问题。
对于日期时间问题,请检查数据库中有什么数据类型的列,有:datetime、datetime2、date...检查您的列是否支持时区

Are you sure than you have to test data layer? I believe NHIbernate was well tested and you have to test you service layer and provide mocks for data.

If it's not a case, set lazy loading to false during your integration testing, it should help resolve UserProxy instead of User issue.
For datetime issue, please check what data type has a column in database, there are: datetime, datetime2, date... check if you column support timezones

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