Reputation: 14219
Half the links I try off the Moq page are broken, including the one for their official API documentation. So I'll ask here.
I have successfully used a single "catch all" parameter like so:
mockRepo.Setup(r => r.GetById(It.IsAny<int>())).Returns((int i) => mockCollection.Where(x => x.Id == i).Single());
However I can't figure out how to achieve the same behavior with multiple parameters.
mockRepo.Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>())).Returns( ..... );
The ....
is the part I can't figure out.
Edit in response to Jordan:
The problem is how to represent the 3 parameters instead of just one.
How to turn:
(int i) => mockCollection.Where(x => x.Id == i)
into:
(int i), (string s), (int j) => mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == j)
Upvotes: 6
Views: 14639
Reputation: 4817
It's pretty much the same as with a single parameter:
.Returns
(
(int i, string s, int x)
=> mockCollection.Where
(
x => x.Id == i
&& x.SomeProp == s
&& x.SomeOtherProp == x
)
);
Or use the generic variant of returns:
.Returns<int, string, int>
(
(i, s, x)
=> mockCollection.Where
(
x => x.Id == i
&& x.SomeProp == s
&& x.SomeOtherProp == x
)
);
Upvotes: 8
Reputation: 5551
I think what you need is:
mockRepo
.Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>()))
.Returns<int,string,int>((id, someProp, someOtherProp) =>
mockCollection.Where(x => x.Id == i && x.SomeProp == s && x.SomeOtherProp == x));
Upvotes: 6
Reputation: 27618
See Mark Seeman's answer to this question:
Settings variable values in a Moq Callback() call
It might help.
Upvotes: 0
Reputation: 1641
Do you mean how do you write the correct lambda?
mockRepo.Setup(r => r.GetByABunchOfStuff(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>()))
.Returns((int i, string s, int i2) => doSomething() );
Upvotes: 2