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?
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;
};
}
}
}
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) => { ... };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With