Search code examples
c#microsoft-fakes

Setting out parameters with Microsoft Fakes


So I'm trying Microsoft Fakes and I like it, but I have a static method with an out parameter and I cannot figure out how to use it:

Static method to fake:

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

        return true;
    }
}

Test:

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

                return false;
            };
        }
    }
}

Now I get an error in my test claiming that my output parameter is wrong ("Cannot resolve symbol 'output'"). I've been trying to get some documentation on how to handle out parameters but I cannot find anything. Anyone had any experience?


Solution

  • As soon as you ask you figure things out. For anyone else having this problem I solved it like this:

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