如何跟踪 Entity Framework Core 事件以进行集成测试?

Ily*_*kov 3 c# entity-framework-core .net-core

我们需要确保基于 EF Core 的代码执行了特定类型的数据库级别的测试操作(例如,任何命令执行或任何事务提交)。

假设应该触发一个真实的数据库,我们不能通过DbContext模拟来隔离它。怎么看:

[Fact]
public async Task Test()
{
    using (var context = new FooContext())
    {
        //context-related code to produce queries and commands
    }

    Assert.True(/* any context-related transaction has been committed */);
}
Run Code Online (Sandbox Code Playgroud)

是否可以?

Ily*_*kov 5

EF Core 不提供自己的跟踪机制。但是,它记录了很多数据库交互事件。我们可以收集这些日志消息并检查它们EventId以确定是否发生了特定操作。以下是 EF Core 使用的关系事件列表:

EF Core 1.1.2:RelationalEventId枚举。

EF Core 2.0.0 预览版 1:RelationalEventId 类(重大更改!)。

我们需要做的就是创建一个假记录器并将其传递给上下文:

[Fact]
public async Task TransactionCommit_Logger_ContainsEvent()
{
    var logger = new FakeLogger();

    var factoryMock = new Mock<ILoggerFactory>();
    factoryMock.Setup(f => f.CreateLogger(It.IsAny<string>()))
        .Returns(logger);

    using (var context = new FooContext(factoryMock.Object))
    {
        using (var transaction = await context.Database.BeginTransactionAsync())
        {
            transaction.Commit();
        }
    }

    Assert.True(logger.Events.Contains((int)RelationalEventId.CommittingTransaction));
}
Run Code Online (Sandbox Code Playgroud)

FakeLogger 将记录的事件 ID 添加到 Events列表中。

public class FakeLogger : ILogger
{
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
        Func<TState, Exception, string> formatter)
    {
        Events.Add(eventId.Id);
    }

    public List<int> Events { get; set; } = new List<int>();

    public bool IsEnabled(LogLevel logLevel) => true;

    public IDisposable BeginScope<TState>(TState state) => null;
}    
Run Code Online (Sandbox Code Playgroud)

称呼 UseLoggerFactory将工厂实例附加到上下文:

public class FooContext : FooParentContext
{
    private readonly ILoggerFactory _loggerFactory;

    public FooContext() { }

    public FooContext(ILoggerFactory loggerFactory)
    {
        _loggerFactory = loggerFactory;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);

        optionsBuilder.UseLoggerFactory(_loggerFactory);
    }
}
Run Code Online (Sandbox Code Playgroud)

PS 您可以更深入地分析日志消息,甚至是 EF 生成的原始 SQL