【问题标题】:Setting out parameters with Microsoft Fakes使用 Microsoft Fakes 设置参数
【发布时间】:2015-09-21 05:02:09
【问题描述】:

所以我正在尝试 Microsoft Fakes,我喜欢它,但我有一个带有 out 参数的静态方法,我不知道如何使用它:

静态造假方法:

public static class Foo
{
    public static bool TryBar(string str, out string stuff)
    {
        stuff = str;

        return true;
    }
}

测试:

[TestFixture]
public class MyTestTests
{
    [Test]
    public void MyTest()
    {
        using (ShimsContext.Create())
        {
            string output;
            ShimFoo.TryBarStringStringOut = (input, out output) =>
            {
                output = "Yada yada yada";

                return false;
            };
        }
    }
}

现在我在测试中收到一个错误,声称我的输出参数错误(“无法解析符号'输出'”)。我一直在尝试获取有关如何处理参数的一些文档,但我找不到任何东西。有人有经验吗?

【问题讨论】:

    标签: c# microsoft-fakes


    【解决方案1】:

    只要你问你就想明白了。对于其他遇到此问题的人,我是这样解决的:

    [TestFixture]
    public class MyTestTests
    {
        [Test]
        public void MyTest()
        {
            using (ShimsContext.Create())
            {
                ShimFoo.TryBarStringStringOut = (string input, out string output) =>
                {
                    output = "Yada yada yada";
    
                    return false;
                };
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      为了澄清这一点,答案是当您的填充方法包含 out 参数时,您需要声明 lambda 表达式的所有参数的类型。

      例如,这将不起作用..

      ShimFoo.TryBarStringStringOut = (input, out output) => { ... };

      这将不起作用......

      ShimFoo.TryBarStringStringOut = (input, out string output) => { ... };

      但是(正如 Maffelu 的回答)这工作......

      ShimFoo.TryBarStringStringOut = (string input, out string output) => { ... };

      【讨论】:

      • 感谢您的补充说明。
      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2012-09-03
      • 2014-01-08
      • 2014-06-19
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多