0

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.

1 Answer 1

0

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());
Sign up to request clarification or add additional context in comments.

1 Comment

Works.I didn't realize that I was actually giving the compiler an Expression<TDelegate> there. I don't think its arguments are treated like method arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.