How to mock Query method in Dapper C# for unit tests?

My method in C# looks like below,

public bool Execute(IDynamicDatabaseConnection dbConn, List<DynamicGrid> loadObjects, out List<DynamicGridValue> result)
{
    result = new List<DynamicGridValue>();

    // Perform the load for each form grid
    foreach (DynamicGrid grid in loadObjects)
    {
            // Execute the stored proc to load the grid data
            IEnumerable<dynamic> storedProcResponse = ExecuteGridDataLoadStoredProcedure(dbConn, storedProcedureName);

            result.Add(new DynamicGridValue(grid, response));
        }
    }

    return true;
}
private IEnumerable<dynamic> ExecuteGridDataLoadStoredProcedure(IDynamicDatabaseConnection dbConn, string storedProcedureName)
{
    const string parameterReturnMessage = "@Return_Message";
    const string parameterDfPKey = "@DFPkey";
    const string parameterParentDfPKey = "@ParentDFPkey";
    const string parameterResult = "@Result";

    var dynamicParameters = new DynamicParameters();

    dynamicParameters.Add(parameterReturnMessage, string.Empty, DbType.String, ParameterDirection.Output, int.MaxValue);
    dynamicParameters.Add(parameterDfPKey, long.TryParse(DFPkey.ToString(), out long dfPkeyInt) ? dfPkeyInt : 0, DbType.Int64, ParameterDirection.Input, int.MaxValue);
    dynamicParameters.Add(parameterParentDfPKey, Convert.ToInt64(ParentDFPkey), DbType.Int64, ParameterDirection.Input, int.MaxValue);
    dynamicParameters.Add(parameterResult, 0, DbType.Int64, ParameterDirection.ReturnValue, int.MaxValue);

    // Retrieve data using Dapper
    var spResponse = dbConn.Query("StoreProcName", dynamicParameters, commandType: CommandType.StoredProcedure);

    // Retrieve the output parameter
    var returnMessage = dynamicParameters.Get<string>("@Return_Message");
    var errorCode = dynamicParameters.Get<int>(parameterResult);

    // Check if there's an error message
    if (!string.IsNullOrEmpty(returnMessage))
    {
        throw new DynamicFormException($"Stored Procedure Error: {returnMessage} (Code: {errorCode})");
    }

    return spResponse;
}

In my unit test,

[TestMethod]
public void Should_ExecuteStoredProcedureAndReturnCorrectXmlResponse()
{
    // Arrange
    var grid = new DynamicGrid
    {
        DataBindingTypeID = (int)GridDataBindingType.StoredProcedure,
        GridCode = "TestGrid",
        DataBindingGetDFStoredProcedureDefinitionID = 1
    };
    var gridDataSource = new DataSource();
    var queryParameterValues = new Dictionary<string, string>();
    var strategy = new DynamicLoadStrategyGridData(grid, gridDataSource, DFPKey, USER_ID, USER_ACCESS_RIGHTS_IDS, queryParameterValues, PARENT_DFPKEY);
    var loadObjects = new List<DynamicGrid> { grid };

    var dbConnMock = A.Fake<IDynamicDatabaseConnection>();

    dynamic spResponse = new ExpandoObject();
    spResponse.key = "Id";
    spResponse.AdditionalData = "12";
    List<dynamic> spResponseMock = new List<dynamic>() { spResponse };

    A.CallTo(() => dbConnMock.Query<dynamic>(A<string>._, A<object>._, A<IDbTransaction>._, A<bool>._, A<int?>._, A<System.Data.CommandType?>._)).Returns(spResponseMock);

    // Act
    var result = strategy.Execute(dbConnMock, loadObjects, out _);

    // Assert
    Assert.IsTrue(result);
}

Why does my spResponse object have count = 0, i.e., there is nothing in it even after setting the mock?
How should I set the mock for

var spResponse = dbConn.Query( storedProcedureName, 
                               dynamicParameters, 
                               commandType: CommandType.StoredProcedure );

this line, the return type for dbConn.Query is IEnumerable<dynamic>.

UPDATE:

Also, tried,

List<dynamic> spResponseMock = new List<dynamic>()
        {
            new Dictionary<string, object>() { { "Id", 1 } }
        };

        // Assign mock response to spResponse
        IEnumerable<dynamic> spResponse = spResponseMock;

        A.CallTo(() => dbConnMock.Query<dynamic>(A<string>._, A<object>._, A<IDbTransaction>._, A<bool>._, A<int?>._, A<System.Data.CommandType?>._)).Returns(spResponse);

But, this also failed.

Try mentioning the anonymous object as an object when mocking.

dbConnMock
.Query<dynamic>(A<string>._, A<object>._, A<System.Data.CommandType?>._))
.Returns(spResponseMock);

Refer this question’s answer for checking each argument in the anonymous object:

How to mock a method call that takes a dynamic object

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật