我的 C# 方法如下所示,
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;
}
在我的单元测试中,
[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);
}
为什么我的
spResponse
对象有count = 0
,即即使设置了模拟之后,里面也没有任何东西?
我应该如何设置模拟
var spResponse = dbConn.Query( storedProcedureName,
dynamicParameters,
commandType: CommandType.StoredProcedure );
这一行,
dbConn.Query
的返回类型是IEnumerable<dynamic>
。
在模拟时尝试将匿名对象作为 1 个对象提及。
dbConnMock.Query<dynamic>(A<string>._, A<object>._, A<System.Data.CommandType?>._)).Returns(spResponseMock);
请参阅此问题的答案来检查匿名对象中的每个参数: 如何模拟采用动态对象的方法调用