Maffelu
Maffelu

Reputation: 2088

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?

Upvotes: 5

Views: 4321

Answers (2)

wh1tet1p
wh1tet1p

Reputation: 61

Just to clarify on this, the answer is that you need to declare the types for all parameters of the lambda expression when your shimmed method contains out parameters.

For example, this will not work..

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

and this will not work...

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

but (as in the answer by Maffelu) this will work...

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

Upvotes: 6

Maffelu
Maffelu

Reputation: 2088

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

Upvotes: 6

Related Questions