简体   繁体   English

使用 EF Core 2.0 时的单元测试

[英]Unit Testing when using EF Core 2.0

For the project I am doing in my internship we have to use a repository pattern.对于我在实习期间所做的项目,我们必须使用存储库模式。 Since the project was already using EF Core and I read that there was already repository and unit of work in the underlying implementation.由于该项目已经在使用 EF Core,并且我读到底层实现中已经有存储库和工作单元。 I figured I would just use the EF Core methods directly without implementing repository classes as I wanted to prevent clutter.我想我会直接使用 EF Core 方法而不实现存储库类,因为我想防止混乱。

Well now I am in a bit of a problem, I can't seem to find much useful information on unit testing when I implement this way.好吧,现在我遇到了一些问题,当我以这种方式实现时,我似乎找不到关于单元测试的很多有用信息。 The requirement is a hard requirement, and I required 100% coverage of CRUD functions within my controllers due to the standards of this firm.该要求是一项硬性要求,由于这家公司的标准,我要求在我的控制器中 100% 覆盖 CRUD 功能。 I found some of the Microsoft Documentation here , here , and the integration testing using SQLite but I still can't seem to figure out unit testing.我在此处此处找到了一些 Microsoft 文档以及使用 SQLite的集成测试,但我似乎仍然无法弄清楚单元测试。 I did some googling as well and I can't find resources aside from integration testing.我也做了一些谷歌搜索,除了集成测试之外我找不到资源。

Is there some way I can unit test using EF Core directly, or am I gonna have to make repositories in order to properly unit test this program?有什么方法可以直接使用 EF Core 进行单元测试,还是我必须制作存储库才能正确地对这个程序进行单元测试?

Since you do not have a separate repository, you do not have to unit test your repository;由于您没有单独的存储库,因此您不必对存储库进行单元测试; you cannot unit test what you don't have.你不能对你没有的东西进行单元测试。

What you would usually test with EF Core would be actual database access.您通常使用 EF Core 测试的是实际的数据库访问。 For that, you can use the in-memory database to temporarily set up your database which you can run full tests against.为此,您可以使用内存数据库来临时设置您可以对其运行完整测试的数据库。 Of course, this is more an integration test than a minimal unit test.当然,这更像是一个集成测试,而不是一个最小的单元测试。

But you should think about this: When you write unit tests, you have to have an actual idea of what you are testing.但是你应该考虑一下:当你编写单元测试时,你必须对你正在测试的内容有一个实际的了解。 Just saying “I need unit tests for component X” does not make real sense.仅仅说“我需要对组件 X 进行单元测试”并没有实际意义。 Usually, it would be more like “I need unit tests for methods U, W, Y of component X”.通常,它更像是“我需要对组件 X 的方法 U、W、Y 进行单元测试”。 So unless you actually have methods with real behavior that you need to test, attempting to write unit tests for the database context just makes no sense.因此,除非您确实拥有需要测试的具有真实行为的方法,否则尝试为数据库上下文编写单元测试是没有意义的。

You only want to unit test things things that you write yourself.您只想对自己编写的东西进行单元测试。 If there's nothing, eg because you are using the direct database context functionalities for CRUD, then there's nothing to test.如果什么都没有,例如因为您正在为 CRUD 使用直接数据库上下文功能,那么就没有什么可测试的。 The EF Core project is already covering their side with unit tests, and you probably don't want to start unit testing third party code here. EF Core 项目已经用单元测试覆盖了他们的一面,您可能不想在这里开始对第三方代码进行单元测试。

With EF Core you may use InMemoryDatabase, mock data of your DbContext and test your methods against this data.使用 EF Core,您可以使用 InMemoryDatabase,模拟 DbContext 的数据并针对此数据测试您的方法。

Repository adds additional abstraction layer and makes testing a bit more transparent, but it is also possible to inject DbContext with mocked data for your unit tests. Repository 添加了额外的抽象层并使测试更加透明,但也可以为单元测试注入带有模拟数据的 DbContext。

Some code to illustrate an idea 一些代码来说明一个想法

Just don't forget to check is InMemoryDatabase was already passed to the context and do not add another provider in that case.只是不要忘记检查 InMemoryDatabase 是否已经传递给上下文,并且在这种情况下不要添加另一个提供程序。

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("ConnectionString");
        }

Tip 提示

If you are using ASP.NET Core, then you should not need this code since your database provider is already configured outside of the context (in Startup.cs).如果您使用的是 ASP.NET Core,那么您应该不需要此代码,因为您的数据库提供程序已经在上下文之外(在 Startup.cs 中)进行了配置。

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

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