3

I'm trying to fake a call to a method with an out parameter, with a ReturnsLazily with some basic logic in it. Ideally, I could assign a value via AssignsOutAndRefParameters based on the ReturnsLazily. However, AssignsOutAndRefParameters only accepts a value up front when the expression is compiled, is there any 'Lazily' type behavior that I'm missing?

Random r = new Random();    

int[] value = new int[] { 1 };
A.CallTo(() => loader.TryLoad(A<int>.Ignored, out value))
    .WithAnyArguments()
    .ReturnsLazily((int key, int[] inValue) =>
    {
        List<int> result = new List<int>();
        if (key > 0)
        {
            for (int i = 0; i < r.Next(100); i++)
            {
                result.Add(r.Next());
            }
        }
        value = result.ToArray();
        return result.Count > 0;
    })
    .AssignsOutAndRefParameters(value); //Assigns [1], instead of [r,a,n,d,o,m,i,n,t,s]
Crutt
  • 524
  • 5
  • 9

1 Answers1

2

Update As @Crutt knows, FakeItEasy 1.22.0+ has AssignsOutAndRefParametersLazily, which supports exactly the desired behaviour:

Random r = new Random();    

int[] value = new int[] { 1 };
A.CallTo(() => loader.TryLoad(A<int>.Ignored, out value))
    .WithAnyArguments()
    .ReturnsLazily((int key, int[] inValue) => key > 0)
    .AssignsOutAndRefParametersLazily((int key, int[] inValue) =>

    {
        List<int> result = new List<int>();
        if (key > 0)
        {
            for (int i = 0; i < r.Next(100); i++)
            {
                result.Add(r.Next());
            }
        }
        return new object[] { result.ToArray() };
    });

WAS:

No, I think you've found a hole where some functionality could be. This appears not to be supported as of FakeItEasy 1.21.0.

I've created Issue 319 over at GitHub to track it.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111