简体   繁体   English

如何在TDD中创建用于框架测试的Entity Framework DbContext?

[英]How to create an Entity Framework DbContext for unit testing in TDD?

I'm in the process of learning about Test Driven Development (TDD) so I can use it in a project I've started, but I've run into a question on how to set up a specific type of test I'd like to do. 我正在学习有关测试驱动开发(TDD)的过程,因此我可以在已经开始的项目中使用它,但是我遇到了一个关于如何设置我想要的特定类型测试的问题去做。

The scenario is that I have a View that allows someone to edit information about a User (such as username, first name, last name, etc). 场景是我有一个视图,该视图允许某人编辑有关用户的信息(例如用户名,名字,姓氏等)。 In many scenarios this User they're editing will already exist in the database, so when they hit save the information gets updated in the database by that View's View Model. 在许多情况下,他们正在编辑的用户将已经存在于数据库中,因此当他们单击保存时,该视图的视图模型会将信息更新到数据库中。

What I'd like to test is that the View Model is saving this information to the database. 我要测试的是,视图模型正在将该信息保存到数据库中。 This is done using an Entity Framework DbContext I pass into the View Model during construction, which means that I need to create a DbContext in the unit test to pass into the View Model that can be updated and compared against. 这是使用在构建过程中传递给视图模型的Entity Framework DbContext完成的,这意味着我需要在单元测试中创建一个DbContext才能传递给可以更新和比较的View模型。

The Assert I'd like to test would be something along the lines of: 我要测试的断言类似于以下内容:

Assert.AreEqual(ViewModelFake.EditedUser.Username, DataBaseContextFake.Users.Find(1).Username);

After the DbContext is originally created by the unit test it populates it with a User, and later in the unit test that User information is changed. 在最初由单元测试创​​建DbContext之后,它将用一个User填充它,随后在单元测试中将User信息更改。 The command that's being tested in the View Model is responsible for saving this edited information into the database, replacing what the DbContext was originally populated with. 在视图模型中正在测试的命令负责将此编辑后的信息保存到数据库中,以替换最初填充DbContext的内容。

I've been searching for a solution for the better part of these last two days but haven't been able to track down examples of people doing the same thing. 在过去两天的大部分时间里,我一直在寻找解决方案,但未能追踪到人们在做同一件事的例子。 Is this something that should even be handled in a unit test? 这是在单元测试中甚至应该处理的东西吗? Please note that I'm not using a repository/unit of work layer on top of Entity Framework. 请注意,我没有在Entity Framework顶部使用存储库/工作单元层。

At work, in our project we created an abstraction over entity framework context. 在工作中,在我们的项目中,我们创建了实体框架上下文的抽象。

Imagine that we have an interface, let's call it IMyCompanyContext and that it exposes all methods that you use from DbContext, including the SaveChanges (); 假设我们有一个接口,我们将其称为IMyCompanyContext,它公开了您从DbContext使用的所有方法,包括SaveChanges();。 method. 方法。 All database-mapped collections are exposed through this interface using the ICollection interface. 使用ICollection接口通过此接口公开所有数据库映射的集合。 The Query provider understands our queries (it checks at runtime that behind that interface is a real DbContext). 查询提供程序理解我们的查询(它在运行时检查该接口后面是否是真正的DbContext)。 More than this, you can use an inversion of control aproach, maybe even using a DI Container for requesting a new IMyCompanyContext. 不仅如此,您还可以使用控制方式的反转,甚至可以使用DI容器来请求新的IMyCompanyContext。

If you want to test this, you just have to implement your context with a bogus context that exposes some lists that you can query. 如果要对此进行测试,只需使用伪造的上下文来实现您的上下文,该伪造的上下文公开一些您可以查询的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM