c# - 在 Xunit 集成测试中检索 ASP.NET Core 3.1 数据库上下文

标签 c# integration-testing xunit asp.net-core-3.1

我一直在使用 Xunit 为使用数据库的 .NET Core 3.1 Web 应用程序创建集成测试。为了进行测试,我已从 Microsoft Documentation 切换到内存数据库。 . CustomWebApplicationFactory 代码是:

public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
        {
        protected override void ConfigureWebHost(IWebHostBuilder webHostBuilder)
            {
            webHostBuilder.ConfigureServices(services =>
                {
                    // Remove the app's database  registration.
                    var serviceDescriptor = services
                                                .SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<MyDbContext>));

                    if (serviceDescriptor != null)
                        {
                        services.Remove(serviceDescriptor);
                        }

                    // Add MyDbContext using an in-memory database for testing.
                    services.AddDbContext<MyDbContext>(options =>
                    {
                        options.UseInMemoryDatabase("InMemoryDbForTesting");
                    });

                    var servicesProvider = services.BuildServiceProvider();

                    // Create a scope to obtain a reference to the database context (MyDbContext).
                    using (var serviceScope = servicesProvider.CreateScope())
                        {
                        var scopedServices = serviceScope.ServiceProvider;
                        var db = scopedServices.GetRequiredService<MyDbContext>();
                        var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();

                        db.Database.EnsureCreated();                                                                                                                                         // Ensure the database is created.

                        try
                            {
                            DatabaseSeeding.voidInitialiseCazIdentityProviderDatabase(db);                                                                                                                              // Seed the database with test data.
                            }
                        catch (Exception ex)
                            {
                            logger.LogError(ex, $"An error occurred seeding the database with data. Error: {ex.Message}");
                            }
                        }
                });
            }

我的基本页面测试在这种安排下工作得很好,但我现在想检查内存数据库是否已被集成测试修改。无论发生什么,对数据库的引用都不会在 Xunit DI 容器中结束(如果存在这种情况)。我的测试类使用以下代码初始化:
    public class IdpUserServiceTests : IClassFixture<CustomWebApplicationFactory<Startup>>
    {
    private readonly CustomWebApplicationFactory<Startup> _webApplicationFactory;
    private readonly ITestOutputHelper _testOutput;
    private readonly HttpClient _httpClient;
    private readonly MyDbContext _myDbContext;

    public IdpUserServiceTests(CustomWebApplicationFactory<Startup> webApplicationFactory, MyDbContext myDbContext, ITestOutputHelper testOutput)
        {
        _webApplicationFactory = webApplicationFactory;
        _myDbContext = myDbContext;
        _testOutput = testOutput;
        _httpClient = _webApplicationFactory.CreateClient();
        }

    //Tests 

但在尝试运行测试时,我收到以下错误:
The following constructor parameters did not have matching fixture data: MyDbContext objMyDbContext

我正在寻找访问内存数据库的正确方法——显然不是通过构造函数注入(inject)。我已经尝试过这个答案 - Access in memory dbcontext in integration test - 但事情似乎在 2.2 和 3.1 之间发生了变化。

最佳答案

事实证明,上面引用的集成测试答案中的 Access in memory dbcontext 对于 ASP.NET Core 3.1 来说并不完全正确。 scopeFactory 必须从不同的来源找到:

var scopeFactory = _webApplicationFactory.Services.GetService<IServiceScopeFactory>();
所以测试类现在看起来像:
public class ServiceTests : IClassFixture<CustomWebApplicationFactory<Startup>>
    {
    private readonly CustomWebApplicationFactory<Startup> _webApplicationFactory;
    private readonly ITestOutputHelper _testOutput;
    private readonly HttpClient _httpClient;

    public ServiceTests(CustomWebApplicationFactory<Startup> webApplicationFactory, ITestOutputHelper testOutput)
        {
        _webApplicationFactory = webApplicationFactory;
        _testOutput = testOutput;
        _httpClient = _webApplicationFactory.CreateClient();
        }

    [Fact]

    public async Task AServiceTest()
        {
        // Do some HTTP client stuff that alters the in-memory database

      var scopeFactory = _webApplicationFactory.Services.GetService<IServiceScopeFactory>();

        using(var scope = scopeFactory.CreateScope())
            {
            var myDataContext = scope.ServiceProvider.GetService<MyDataContext>();

            // Query the in-memory database
            }
        }
    }

关于c# - 在 Xunit 集成测试中检索 ASP.NET Core 3.1 数据库上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62048264/

相关文章:

java - Google App Engine 数据存储集成测试

cakephp - 如何在 CakePHP 集成测试中获取请求的响应?

c# - 单元测试的可重用设置

.net-core - NET CORE 2.0 的 Console.WriteLine 在 Windows 上的 xUnit 中不起作用

c# - ObjectDataSource 的 Select 方法中 ListBox 为 null

c# - 使用C#从xml文件中获取值

python - 如何在 C# 中返回 python 脚本的 "return value"?

java - 无法在 Controller Spring-boot 中测试需要身份验证的方法

c# - 单元测试无限轮询方法

c# - 在 AccountController 之外配置 UserManager 和 UserStore?