如何在ASP.NET核心集成测试中测试数据库操作



我正在尝试集成我的应用程序。

例如,在我的 AbController中,我有 PostAb(AbDTO abDTO)方法,我想测试调用此方法会将abdto添加到db。

现在我的测试设置:

    [SetUp]
    public void SetUp()
    {
        _server = new TestServer(new WebHostBuilder()
            .UseEnvironment("testing")
            .UseStartup<Startup>());
        _client = _server.CreateClient();
    }

我的测试应该看起来像:

    [Test]
    public async Task PostAbSanity()
    {
        await _client.PostAsync("/rest/v1/Ab", new Ab{Id=1});
        _context.Abs.find(1).should().NotBeNull();
    }

但是,如何将_context注入测试?在我的应用程序中,我通过构造函数注入它,但是在测试中,我不能。

谢谢!

我假设您正在使用sqllite,因此您可以编辑这样的设置方法:

[SetUp]
public void SetUp()
{
    _server = new TestServer(new WebHostBuilder()
        .UseEnvironment("testing")
        .UseStartup<Startup>());
    _client = _server.CreateClient();
    var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
    optionsBuilder.UseSqlite("Filename=./blog.db");
    _context = new BloggingContext(optionsBuilder.Options);
}

请让我知道这是否有用

我承认,与内存数据库进行集成测试是最透彻和安全的工作方式,但我的具体情况将使这方面变得非常困难且耗时。我正在使用一段测试后覆盖的托管SQL开发服务器。我工作了几天,发现以下是给我寻找结果的过程。

  • dotnet core 2.1
  • 六边形(洋葱)架构需要测试我数据中写的业务逻辑访问层

program.cs文件我添加了:

//for integration testing
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .ConfigureServices(services => services.AddAutofac())
    .UseStartup<Startup>();

我的集成测试文件:

using Core.Data.Entities.Model;
using Core.Data.Entities.UTIA;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Profile.Data.Repos;
using Profile.Domain.DomainObjects;
using Super2.Web;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Profile.Test
{
    public class EmployeeProfileIntegrationTest : IClassFixture<WebApplicationFactory<Startup>>
    {
        private readonly HttpClient _client;
        public EmployeeProfileIntegrationTest(WebApplicationFactory<Startup> factory)
        {
            _client = factory.CreateClient();
        }
        private DBContext GetContext()
        {
            var options = new DbContextOptionsBuilder<DBContext>()
                .UseSqlServer("Server = 'Connection String from appsettings'")
                .Options;
            var context = new DBContext(options);
            return context;
        }

        [Fact]
        public async Task TestChildProtectionEmployeeGetData()
        {
            //Arrange
            var ApplicationUserId = XXXX;
            var repo = new EmployeeProfileRepo(GetContext());
            //ACT
            var sut = await repo.GetChildProtectionHistory(ApplicationUserId);
            //Assert
            var okResult = sut.Should().BeOfType<List<DomainObject>>().Subject;
            okResult.First().ApplicationUserId.Should().Be(XXXX);
        }
    }
}

当我将上下文注入不同的层时,我会怀疑它对控制器的上下文也相同。我包括了启动片段,因为这引起了我的一些问题,因为TestServer正在寻找IWEBHOSTBUILDER,而不是默认的Core2.1 IWEBHOST。

无论哪种方式,这都对我有用。希望您可以从中获得一些帮助。

最新更新