FakeItEasy ReturnLazily with more than 4 arguments method

自闭症网瘾萝莉.ら 提交于 2019-12-08 00:59:02

问题


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:


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.



来源:https://stackoverflow.com/questions/31018199/fakeiteasy-returnlazily-with-more-than-4-arguments-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!