首页 > 解决方案 > 如何使用 Moq.Dapper 模拟 QueryMultiple

问题描述

我正在编写单元测试用例,并且成功地为Query. 但是我没有为QueryMultiple.

对于查询,我这样写:

 IEnumerable<ClientTestPurpose> fakeTestPurposes = new 
 List<ClientTestPurpose>()
 {
      new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name1"},
      new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name2"},
      new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name3"}
 };

 _mock.SetupDapper(x => x.Query<ClientTestPurpose>(It.IsAny<string>(), null, null, true, null, null)).Returns(fakeTestPurposes);

 var result = _libraryRepository.TestPurposes(clientModal.Id);

 Assert.IsNotNull(result);
 Assert.AreEqual(result.Count(), fakeTestPurposes.Count());   

如何写QueryMultiple

using (var multi = _db.QueryMultiple(spName, spParams, commandType: CommandType.StoredProcedure))
{
     var totals = multi.Read<dynamic>().FirstOrDefault();
     var aggregates = multi.Read<StatusModel>();
     var scripts = multi.Read<LibraryItemModel>();
     var runs = multi.Read<RunSummaryModel>();
     var filteredTotals = multi.Read<dynamic>().FirstOrDefault();
}

标签: unit-testingmoqdapper

解决方案


显然你使用 Moq.Dapper 扩展。这是SetupDapperSetupDapperAsync方法的代码:

public static ISetup<IDbConnection, TResult> SetupDapper<TResult>(this Mock<IDbConnection> mock, Expression<Func<IDbConnection, TResult>> expression)
{
  MethodCallExpression body = expression.Body as MethodCallExpression;
  if ((body != null ? body.Method.DeclaringType : (Type) null) != typeof (SqlMapper))
    throw new ArgumentException("Not a Dapper method.");
  string name = body.Method.Name;
  if (name == "Execute")
    return (ISetup<IDbConnection, TResult>) DbConnectionInterfaceMockExtensions.SetupExecute(mock);
  if (name == "ExecuteScalar")
    return DbConnectionInterfaceMockExtensions.SetupExecuteScalar<TResult>(mock);
  if (name == "Query" || name == "QueryFirstOrDefault")
    return DbConnectionInterfaceMockExtensions.SetupQuery<TResult>(mock);
  throw new NotSupportedException();
}

public static ISetup<IDbConnection, Task<TResult>> SetupDapperAsync<TResult>(this Mock<IDbConnection> mock, Expression<Func<IDbConnection, Task<TResult>>> expression)
{
  MethodCallExpression body = expression.Body as MethodCallExpression;
  if ((body != null ? body.Method.DeclaringType : (Type) null) != typeof (SqlMapper))
    throw new ArgumentException("Not a Dapper method.");
  if (body.Method.Name == "QueryAsync")
    return DbConnectionInterfaceMockExtensions.SetupQueryAsync<TResult>(mock);
  throw new NotSupportedException();
}

如您所见,Moq.Dapper 仅支持对Execute、和方法进行模拟。因此,您可能会继续尝试 mock 。正如@TrueWill 在评论中所说,要模拟 DB 行为,您可能需要首先引入另一个抽象级别。这只是一个示例,说明您的情况如何:ExecuteScalarQueryQueryAsyncNotSupportedExceptionQueryMultiple

[Test]
public void DoSomethingWithQueryTest()
{
    // Arrange
    IEnumerable<ClientTestPurpose> fakeTestPurposes = new
        List<ClientTestPurpose>
        {
            new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name1" },
            new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name2" },
            new ClientTestPurpose { PurposeID = 1, PurposeName = "Test Purpose name3" }
        };

    var mock = new Mock<ILibraryRepository>();
    mock.Setup(x => x.TestPurposes(It.IsAny<int>())).Returns(fakeTestPurposes);
    var logicService = new SomeLogicService(mock.Object);

    // Act
    var result = logicService.DoSomethingWithQuery(1);

    // Assert
    Assert.IsNotNull(result);
    Assert.AreEqual(result.Count(), fakeTestPurposes.Count());
}

[Test]
public void DoSomethingWithQueryMultipleTest()
{
    // Arrange
    SomeAggregate fakeTestPurposes = new SomeAggregate();

    var mock = new Mock<ILibraryRepository>();
    mock.Setup(x => x.TestQueryMultiple()).Returns(fakeTestPurposes);
    var logicService = new SomeLogicService(mock.Object);

    // Act
    var result = logicService.DoSomethingWithQueryMultiple();

    // Assert
    Assert.IsNotNull(result);
}

public interface ILibraryRepository
{
    IEnumerable<ClientTestPurpose> TestPurposes(int id);
    SomeAggregate TestQueryMultiple();
}

public class LibraryRepository : ILibraryRepository
{
    private readonly IDbConnection _db;

    public LibraryRepository(IDbConnection db)
    {
        _db = db ?? throw new ArgumentNullException(nameof(db));
    }

    public IEnumerable<ClientTestPurpose> TestPurposes(int id)
    {
        return _db.Query<ClientTestPurpose>("SQL here", new { id }, null, true, null, null);
    }

    public SomeAggregate TestQueryMultiple()
    {
        string spName = "SQL here";
        var spParams = new { Id = 1 };
        using (var multi = _db.QueryMultiple(spName, spParams, commandType: CommandType.StoredProcedure))
        {
            return new SomeAggregate
            {
                totals = multi.Read<dynamic>().FirstOrDefault(),
                aggregates = multi.Read<StatusModel>(),
                scripts = multi.Read<LibraryItemModel>(),
                runs = multi.Read<RunSummaryModel>(),
                filteredTotals = multi.Read<dynamic>().FirstOrDefault()
            };
        }
    }
}

public class SomeAggregate
{
    public IEnumerable<dynamic> totals { get; set; }
    public IEnumerable<StatusModel> aggregates { get; set; }
    public IEnumerable<LibraryItemModel> scripts { get; set; }
    public IEnumerable<RunSummaryModel> runs { get; set; }
    public IEnumerable<dynamic> filteredTotals { get; set; }
}

/// <summary>
/// Example logic server, that just returns results from repository
/// </summary>
public class SomeLogicService
{
    private readonly ILibraryRepository _repo;

    public SomeLogicService(ILibraryRepository repo)
    {
        _repo = repo;
    }

    public IEnumerable<ClientTestPurpose> DoSomethingWithQuery(int id)
    {
        return _repo.TestPurposes(id);
    }

    public SomeAggregate DoSomethingWithQueryMultiple()
    {
        return _repo.TestQueryMultiple();
    }
}

主要思想是将所有数据库特定的东西隐藏在后面,ILibraryRepository并将您需要测试的所有逻辑移动到某个逻辑服务器,该逻辑服务器将接收存储库作为依赖项。为了存储库中的代码应该简单、明显,包含所有 DB 特定逻辑:连接、事务、命令、对象关系映射等。并且您不需要使用 unt 测试覆盖此代码。但是,您确实涵盖SomeLogicService了单元测试的代码,因为这是您真正需要测试的内容。您会看到 Dapper 扩展方法是相当低级的抽象,它不会隐藏使用 DB 的细节,它们只是帮手。希望能帮助到你。


推荐阅读