简体   繁体   English

Mocking 带有 output 参数的存储过程

[英]Mocking a stored procedure with an output parameter

I am having following method:我有以下方法:

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;
    }

this method calls a stored procedure using LinqToDB and i am passing a parameter array which consists of 3 input parameters and one output parameter.此方法使用 LinqToDB 调用存储过程,我正在传递一个参数数组,该数组由 3 个输入参数和一个 output 参数组成。 i am having problem writing unit test for this.我在为此编写单元测试时遇到问题。 I am using MOQ framework.我正在使用最小起订量框架。

Up on execution, this stored procedure stores either 'C' or 'E' into the output parameter 'statusOutputParameter'.在执行时,此存储过程将“C”或“E”存储到 output 参数“statusOutputParameter”中。

I have tried:我努力了:

        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;
            });

the above unit test is getting passed but i am not sure how to return either C or E to 'statusOutputParameter' output parameter.上面的单元测试正在通过,但我不确定如何将 C 或 E 返回到“statusOutputParameter”output 参数。

Sorry, but I was struggling with the same issue and found the solution.抱歉,但我一直在努力解决同样的问题并找到了解决方案。 Please try with a query parameter.请尝试使用查询参数。

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

        MockMerretDataContext.Setup(m => m.ExecuteStoredProcedure("MBLABL", It.IsAny<QueryParameter[]>()))
            .Callback((string sp, QueryParameter[] cdp) => 
            {
                // This returns the required value from @STATUS
                QueryParameter[] paramArrayTemp = (QueryParameter[])cdp;
                paramArrayTemp.Find<QueryParameter>(p => p.name == "@STATUS").value = RETURN_VALUE;
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM