Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq a function with 5+ parameters and access invocation arguments

I have a function I want to Moq. The problem is that it takes 5 parameters. The framework only contains Action<T1,T2,T3,T4> and Moq's generic CallBack() only overloads Action and the four generic versions. Is there an elegant workaround for this?

This is what I want to do:

public class Filter : IFilter  
{  
    public int Filter(int i1, int i2, int i3, int i4, int i5){return 0;}  
}

//Moq code:
var mocker = new Mock<IFilter>();  
mocker.Setup(x => x.Filter(  
    It.IsAny<int>(),  
    It.IsAny<int>(),  
    It.IsAny<int>(),  
    It.IsAny<int>(),  
    It.IsAny<int>(),  
    It.IsAny<int>())  
.Callback
(  
    (int i1, int i2, int i3, int i4, int i5) => i1 * 2  
);  

Moq doesn't allow this because there is no generic Action that takes 5+ parameters. I've resorted to making my own stub. Obviously, it would be better to use Moq with all of its verifications, etc.

like image 457
beerncircus Avatar asked Mar 29 '10 20:03

beerncircus


1 Answers

This is supported in the final release of Moq 4.0 (4.0.10827), which was released on April 12, 2011. If you're using .NET 4, you'll be able to mock up to 16 parameters.

like image 190
Rory MacLeod Avatar answered Sep 17 '22 13:09

Rory MacLeod