Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FakeItEasy AssignsOutAndRefParameters - lazily?

Tags:

fakeiteasy

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]
like image 407
Crutt Avatar asked Jan 24 '26 01:01

Crutt


1 Answers

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.

like image 109
Blair Conrad Avatar answered Jan 27 '26 01:01

Blair Conrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!