Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用输出参数模拟存储过程_C#_Moq - Fatal编程技术网

C# 使用输出参数模拟存储过程

C# 使用输出参数模拟存储过程,c#,moq,C#,Moq,我有以下方法: public IEnumerable<string> PrintMasterBagAssignmentManifest(int companyId, int bagNo) { var errors = new List<string>(); try { if (companyId > 0 && bagNo > 0) {

我有以下方法:

public IEnumerable<string> PrintMasterBagAssignmentManifest(int companyId, int bagNo)
    {
        var errors = new List<string>();
        try
        {
            if (companyId > 0 && bagNo > 0)
            {
                var companyNumber = companyId.ToString("D2");
                var bagNumber = bagNo.ToString("D8");

                DataParameter compParameter = new DataParameter("COMP", companyNumber);
                DataParameter bagParameter = new DataParameter("BAG", bagNumber);
                DataParameter userParameter = new DataParameter("USER", UserName);
                DataParameter statusOutputParameter = new DataParameter("STATUS", "");
                statusOutputParameter.Direction = System.Data.ParameterDirection.Output;
                DataParameter[] param = new DataParameter[] {
                    compParameter,
                    bagParameter,
                    userParameter,
                    statusOutputParameter
                };

                MerretDataContext.ExecuteStoredProcedure("MBMANF", param);

                if (statusOutputParameter.Value.ToString() == "E" && string.IsNullOrEmpty(statusOutputParameter.Value.ToString()))
                {
                    errors.Add(string.Format(Resources.Resources.MASTERBAG_PRINTMANIFEST_ERROR, bagNo));
                }
            }
            else
            {
                errors.Add(Resources.Resources.MASTERBAG_INVALID);
            }
        }
        catch (Exception ex)
        {
            errors.Add(ex.Message);
        }
        return errors;
    }
public IEnumerable PrintMasterBagAssignmentManifest(int companyId,int bagNo)
{
var errors=新列表();
尝试
{
如果(公司ID>0&&bagNo>0)
{
var companyNumber=公司ID.ToString(“D2”);
var bagNumber=bagNo.ToString(“D8”);
DataParameter compParameter=新数据参数(“COMP”,companyNumber);
DataParameter bagParameter=新数据参数(“行李”,行李编号);
DataParameter userParameter=新的DataParameter(“用户”,用户名);
DataParameter statusOutputParameter=新的DataParameter(“状态”,“状态”);
statusOutputParameter.Direction=System.Data.ParameterDirection.Output;
DataParameter[]param=新的DataParameter[]{
compParameter,
bagParameter,
用户参数,
状态输出参数
};
MerretDataContext.ExecuteStoredProcedure(“MBMANF”,param);
if(statusOutputParameter.Value.ToString()==“E”&&string.IsNullOrEmpty(statusOutputParameter.Value.ToString())
{
errors.Add(string.Format(Resources.Resources.MASTERBAG_printmifest_ERROR,bagNo));
}
}
其他的
{
errors.Add(Resources.Resources.MASTERBAG_无效);
}
}
捕获(例外情况除外)
{
错误。添加(例如消息);
}
返回错误;
}
这个方法使用LinqToDB调用一个存储过程,我传递一个参数数组,该数组由3个输入参数和一个输出参数组成。我在为此编写单元测试时遇到问题。我正在使用MOQ框架

执行时,此存储过程将“C”或“E”存储到输出参数“statusOutputParameter”中

我试过:

        const string RETURN_VALUE = "C";
        DataParameter[] dp = null;

        MockMerretDataContext.Setup(m => m.ExecuteStoredProcedure("MBLABL", It.IsAny<DataParameter[]>()))
            .Callback((string sp, DataParameter[] cdp) => 
            {
                dp = cdp;
                cdp[3].Value = RETURN_VALUE;
            });
const string RETURN\u VALUE=“C”;
DataParameter[]dp=null;
MockMerretDataContext.Setup(m=>m.ExecuteStoredProcedure(“MBLABL”,It.IsAny()))
.Callback((字符串sp,数据参数[]cdp)=>
{
dp=cdp;
cdp[3]。值=返回值;
});

上述单元测试正在通过,但我不确定如何将C或E返回到“statusOutputParameter”输出参数。

“我在为此编写单元测试时遇到问题”。你能说得更具体些吗?你试过什么,试了会遇到什么问题?嗨,马克,谢谢你的回答。我更新了一些更多的信息。请看一看。您可以在回调中指定具体值或执行逻辑,而不是对任何参数执行它。请指定要模拟的用例。调用存储过程后,我希望模拟“IF”条件:IF(statusOutputParameter.Value.ToString()==“E”&&string.IsNullOrEmpty(statusOutputParameter.Value.ToString())