FakeItEasy ReturnLazily with more than 4 arguments method

1.1k Views Asked by At

With FakeItEasy, I want to fake an interface method to return some custom list, the method has more than 4 arguments, signature of method is this:

     IList<Employee> FindAll(DateTime dateFrom, DateTime dateTill, Guid locationId, Gender gender, int age);

While FakeItEasy has ReturnsLazily method which has supports till only 4 arguments, so for this 5 arguments method I cannot use ReturnsLazily functionality.

    A.CallTo(() => repAssign.FindAll(A<DateTime>.Ignored,A<DateTime>.Ignored,A<Guid>.Ignored,A<Gender>.Ignored,A<Int>.Ignored))
         .ReturnsLazily((DateTime StartDate, DateTime EndDate, Guid locationId, Gender gender, int age) =>
         return list.Where(...some filters here as per arguments...).ToList();
        );

With FakeItEasy, please suggest how to use ReturnsLazily for methods which has more than 4 arguments.

1

There are 1 best solutions below

1
On BEST ANSWER

You need to use overload that takes IFakeObjectCall as lambda parameter and extract arguments from there:

If more advanced decision-making is required, or the method has more than 4 parameters, the convenience methods won't work. Use the variant that takes an IFakeObjectCall instead

 A.CallTo(() => repAssign.FindAll(
     A<DateTime>.Ignored,
     A<DateTime>.Ignored,
     A<Guid>.Ignored,
     A<Gender>.Ignored,
     A<Int>.Ignored))
.ReturnsLazily(call => 
    list
        .Where(x => call.GetArgument<Int>(4) > 1)
        .ToList()
);

Arguments can be extracted via GetArgument generic method which takes argument type as generic parameter and argument position (0-based).

On a side note, you should consider refactoring of such method. This many parameters are better wrapped into parameter object class:

var filterParameters = new FilterParameters
{
    LocationGuid = guid,
    Gender = Gender.Female,
};
var result = repo.FindAll(filterParameters);

This will make introduction of new parameters much easier and will help avoid issues like this one.