Sunday 16 August 2020

Xunit Mocking methods setup with Action<> parameter

I did setup a callback method in setup with Action<> parameter as onSuccess and onFailure callback. After seamless implementation i was trying to setup moq for the same method. And i finished everything up by using following code snippet.


Method template:

void ValidateUserAccess(List<int> conpanyIds, int? userId = null, Action<List<int>> onSuccess = null, Action<List<int>> onFailure = null, bool allowPartialExecution = false);


Moq Setup:

 _mqUserService.Setup(m => m.ValidateUserAccess(
                 It.IsAny<List<int>>(),
                 It.IsAny<int>(),
                 It.IsAny<Action<List<int>>>(),
                 It.IsAny<Action<List<int>>>(),
                 It.IsAny<bool>()
                ))
                .Callback((List<int> ids, int? userId , Action<List<int>> onSuccess , Action<List<int>> onFailure , bool isExecteall) =>
                {
                   onSuccess(ids);
//Other stuff to validate
                })
                .Verifiable();