如何执行集成测试来测试急切加载的实体(EF4 Code First)
这是一个简单的场景来解释我想要做什么。假设我正在创建一个博客引擎,并且有 2 个实体:Post
和 Comment
,两者之间具有一对多关系。在我的服务层中,我有一个查询逻辑来检索有关帖子的详细信息,如下所示:
Post post = new PostByIdQuery(_unitOfWork).WithPostId(5).Execute();
该行代码将执行一个查询,该查询将从数据库中检索 id 值为 5 的帖子实体。该查询对象已经是使用真实数据库进行编码并通过集成测试。
如果我正在编辑帖子或显示带有评论的帖子,则有两个业务流程,我可能希望通过指定的 ID 获取帖子。此查询对象适用于这两种情况,但在显示带有评论的帖子时会产生性能影响,因为默认情况下评论列表是延迟加载的。因此,在迭代帖子的评论时将导致多次数据库命中。
当然,如果我总是急切地加载帖子的评论,如果我只是编辑帖子,则会导致不必要的表连接。
因此,我想向流畅的界面添加一个新方法,该方法将指定评论是否应该延迟加载或问题是,如何编写一个集成测试来检查注释表是否已急切加载,以便在运行单元/集成测试时可以检查此新要求
? Post.Comments 属性在访问时将显示相同的内容,无论是急切加载还是延迟加载,因此我不确定如何为此创建测试。
Edit: As an FYI, this is using the Code-First mechanism of EF4, thus my entities are POCOs.
Here is a simple scenario to explain what I am trying to do. Say I am creating a Blogging engine, and I have 2 entities, Post
and Comment
, with a 1-to-many relationship between the two. In my service layer, I have a query logic to retrieve the details about a post that goes like:
Post post = new PostByIdQuery(_unitOfWork).WithPostId(5).Execute();
That line of code will execute a query that will retrieve the post entity from the database with an id value of 5. This query object is already coded and passes integration tests using a real database.
There are two business processes in which I may want to get a post by a specified ID, if I am editing a post or when I am displaying a post with its comments. This query object works fine for both scenarios, but there are performance implications when displaying a post with it's comments since the comment list is by default lazy loaded. Thus while iterating through the comments for a post will cause multiple database hits.
Of course, if I always eager load the comments for a post, if I"m just editing a post it causes unnecessary table joins.
Thus I want to add a new method to the fluent interface that will specifies if comments should be lazy loaded or not. The question is, how do I write an integration test that checks if the comment table is eager loaded or not so this new requirement can be checked whenever unit/integration tests are run?
As far as I can tell, the Post.Comments
property will show the same when accessed whether it's eager loaded or lazy loaded, so I'm not sure how to create a test for this.
Edit: As an FYI, this is using the Code-First mechanism of EF4, thus my entities are POCOs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有可能将集合对象转换为更高级的 EntityCollection 类型,然后检查其中的 IsLoaded 属性。
如果这不起作用,请看看罗文对我不久前提出的这个问题的回答。出于不同的原因,我试图将我的代码优先集合公开为 EntityCollection。
在 CTP4 Code First 中使用 CreateSourceQuery
There's a chance you'll be able to cast the collection object to the more advanced EntityCollection type, and then check the IsLoaded property therein.
If that doesn't work, have a look at Rowan's answer to this question I asked a while ago. I was trying to get my code-first collection to be exposed as EntityCollection for a different reason.
Using CreateSourceQuery in CTP4 Code First