I have a generic C# interface that I'm trying to mock with FakeItEasy for an xUnit test like this:
#region FakeItEasyDebugging
public interface IFakeTest<T> : IDisposable where T : new()
{
/// <summary>
/// Gets data.
/// </summary>
/// <param name="key">Data key</param>
/// <param name="data">Data from database, null if not found</param>
/// <returns>Returns True if found</returns>
bool GetData(string key, out T data);
}
public class TestClass
{
public int Foo { get; set; } = 0;
}
public class FakeTest
{
[Fact]
public void TestFakeItEasy()
{
const string testKey = "some";
IFakeTest<TestClass> testObj = A.Fake<IFakeTest<TestClass>>();
A.CallTo(() => testObj.GetData(testKey, out data)).Returns(true).AssignsOutAndRefParameters(new TestClass());
}
}
#endregion
But, this doesn't work even though it looks a lot like what the docs would instruct me to do:
https://fakeiteasy.github.io/docs/8.0.0/assigning-out-and-ref-parameters/
The error message I get is CS0103: The name 'data' does not exist in the current context.
I'm not getting any useful suggestions from Visual Studio either.
So, how should I write the CallTo line to actually make it work and get to specify how the mock IFakeTest.GetData method is to behave?
A.CallTo(() => testObj.GetData(testKey, out TestClass data)).Returns(true).AssignsOutAndRefParameters(new TestClass());
Yields CS8198.
A.CallTo(() => testObj.GetData(testKey, out TestClass)).Returns(true).AssignsOutAndRefParameters(new TestClass());
Yields CS0118.
As you note, the compiler is complaining to you because you haven't defined data
anywhere. Whether FakeItEasy was being used or not, GetData(testKey, out data)
won't compile without declaring data
.
TestClass data;
A.CallTo(() => testObj.GetData(testKey, out data)).Returns(true).AssignsOutAndRefParameters(new TestClass());