0

My method in C# looks like this:

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 procedure 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("StoredProcedureName", 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:

I 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.

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

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); Tried this but it failed as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.