简体   繁体   English

使用 ASP.NET 内核和 NUnit 实施集成测试

[英]Implement Integration Testing with ASP.NET Core and NUnit

I am new to use NUnit integration testing with Asp core 2.2, I have already core RESTful Api but have no idea how to configure NUnit to implement integration test.我是使用 Asp core 2.2 的 NUnit 集成测试的新手,我已经拥有核心 RESTful Api 但不知道如何配置 NUnit 来实现集成测试。 Usually I Implement it using Configuration file from Migration Folder as the following but no more exists in ASP Core 2.2 and I don't know what the new alternative.通常我使用迁移文件夹中的配置文件来实现它,但在 ASP Core 2.2 中不再存在,我不知道新的替代方案是什么。

        var configuration = new Migrations.ApplicationDbContextModelSnapshot();
        var migrator = new Migrator(configuration);
        migrator.Update();

So please If you don't understand my question I just need an explicit link to use NUnit Integration Testing with.Net Core 2.2.因此,如果您不理解我的问题,我只需要一个明确的链接即可将 NUnit 集成测试与 .Net Core 2.2 一起使用。

An integration test should not be connecting to a real database.集成测试不应连接到真实数据库。 Integration testing is about ensuring components function together correctly, not any concrete backend implementation.集成测试是关于确保组件 function 正确组合在一起,而不是任何具体的后端实现。 As such, you should be using the in-memory database provider for EF Core.因此,您应该使用 EF Core 的内存数据库提供程序。 This will automatically "migrate" (really it's just setting up a representation of what your database looks like in memory each time it's instantiated), so it's not necessary to take any further action.这将自动“迁移”(实际上它只是在每次实例化时设置数据库在 memory 中的样子),因此无需采取任何进一步的措施。 Just keep in mind that EF-in-memory database is also a non-relational database, so if you want to keep the relational integrity of your entities like the foreign keys, you should use SqlLite in-memory database.请记住,EF-in-memory 数据库也是一个非关系型数据库,所以如果你想保持实体的关系完整性,比如外键,你应该使用 SqlLite 内存数据库。 Below is an example:下面是一个例子:

var connection = new SqliteConnection("Data Source=:memory:");
services.AddDbContext<WebApi1DbContext>(options => options.UseSqlite(connection));

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

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