更改 NSubstitute 中的参数值
Change value of parameter in NSubstitute
我有这个方法可以用 NSubstitute 模拟:
public T VoerStoredProcedureUit<T>(string naam, params SqlParameter[] parameters)
使用它的测试方法向该方法发送 2 个 SqlParameters。 VoerStoredProcedureUit 应该更改这些参数的值,以便测试的方法可以提取它。
我用 NSubstitute 创建了以下内容:
SqlParameter[] param =
{
new SqlParameter("@pat_id", SqlDbType.BigInt) {Direction = ParameterDirection.Output, Value = "Melding"},
new SqlParameter("@Melding", SqlDbType.VarChar, 4096) {Direction = ParameterDirection.Output, Value = 2}
};
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs(x =>
{
x[1] = param;
return PatientNieuwResultaat.Succes;
});
然而设置出现异常:
A first chance exception of type 'NSubstitute.Exceptions.ArgumentIsNotOutOrRefException' occurred in NSubstitute.dll
Additional information: Could not set argument 1 (SqlParameter[]) as it is not an out or ref argument.
如果方法隐含地使用引用值,你如何return一个值?
如果我对你的问题的理解正确,那么当调用 VoerStoredProcedureUit<PatientNieuwResultaat>
时,你正试图 return param
的内容。
在ReturnsForAnyArgs
中,x[1]
指的是第二个参数SqlParameter[]
。这不是一个 ref/out 参数,所以你不能在调用者中重新分配它,这就是你得到错误的原因。相反,您需要将模板中的元素复制到提供的数组中。像这样:
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs((x) =>
{
for (int i = 0; i < param.Length; i++)
{
((SqlParameter[])x[1])[i] = param[i];
}
return PatientNieuwResultaat.Succes;
});
您显然可以删除 for 循环,因为您知道需要复制多少参数...
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs((x) =>
{
((SqlParameter[])x[1])[0] = param[0];
((SqlParameter[])x[1])[1] = param[1];
return PatientNieuwResultaat.Succes;
});
我找到了可行的解决方案。为参数分配一个新变量不知何故不起作用,但改变它们却起作用了。另外,方法参数的第二个是一个数组,所以应该这样处理。
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs(x =>
{
paramPatId = ((SqlParameter[])x[1])[0];
paramMelding = ((SqlParameter[])x[1])[1];
paramPatId.Value = (long)2;
paramMelding.Value = "Melding";
return PatientNieuwResultaat.Succes;
});
我有这个方法可以用 NSubstitute 模拟:
public T VoerStoredProcedureUit<T>(string naam, params SqlParameter[] parameters)
使用它的测试方法向该方法发送 2 个 SqlParameters。 VoerStoredProcedureUit 应该更改这些参数的值,以便测试的方法可以提取它。
我用 NSubstitute 创建了以下内容:
SqlParameter[] param =
{
new SqlParameter("@pat_id", SqlDbType.BigInt) {Direction = ParameterDirection.Output, Value = "Melding"},
new SqlParameter("@Melding", SqlDbType.VarChar, 4096) {Direction = ParameterDirection.Output, Value = 2}
};
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs(x =>
{
x[1] = param;
return PatientNieuwResultaat.Succes;
});
然而设置出现异常:
A first chance exception of type 'NSubstitute.Exceptions.ArgumentIsNotOutOrRefException' occurred in NSubstitute.dll
Additional information: Could not set argument 1 (SqlParameter[]) as it is not an out or ref argument.
如果方法隐含地使用引用值,你如何return一个值?
如果我对你的问题的理解正确,那么当调用 VoerStoredProcedureUit<PatientNieuwResultaat>
时,你正试图 return param
的内容。
在ReturnsForAnyArgs
中,x[1]
指的是第二个参数SqlParameter[]
。这不是一个 ref/out 参数,所以你不能在调用者中重新分配它,这就是你得到错误的原因。相反,您需要将模板中的元素复制到提供的数组中。像这样:
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs((x) =>
{
for (int i = 0; i < param.Length; i++)
{
((SqlParameter[])x[1])[i] = param[i];
}
return PatientNieuwResultaat.Succes;
});
您显然可以删除 for 循环,因为您知道需要复制多少参数...
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs((x) =>
{
((SqlParameter[])x[1])[0] = param[0];
((SqlParameter[])x[1])[1] = param[1];
return PatientNieuwResultaat.Succes;
});
我找到了可行的解决方案。为参数分配一个新变量不知何故不起作用,但改变它们却起作用了。另外,方法参数的第二个是一个数组,所以应该这样处理。
productieVerbinding.VoerStoredProcedureUit<PatientNieuwResultaat>(Arg.Any<string>(),
Arg.Any<SqlParameter[]>()).ReturnsForAnyArgs(x =>
{
paramPatId = ((SqlParameter[])x[1])[0];
paramMelding = ((SqlParameter[])x[1])[1];
paramPatId.Value = (long)2;
paramMelding.Value = "Melding";
return PatientNieuwResultaat.Succes;
});