Reputation: 9411
I currently have such a Moq expression
repo.Setup(r => r.GetProjectConfigurationById(It.Is<int>(s => s == response.Id)))
.Returns(response); // return response only if id matches setup one
As one can see, response is an object that has its own Id field.
Now I have a List<responses>
and would like to transfer this expression into something that behaves as such:
Id
Id
is mathcing a response.Id
, return that element of a list.null
How I could do that with Moq?
Upvotes: 2
Views: 1337
Reputation: 139768
You can use the It.IsAny<int>()
to match any parameter in GetProjectConfigurationById
There are also overloads of the Returns
function where you can specify your custom condition using the parameter passed in to your GetProjectConfigurationById
to look up the element by id or return null:
var responses = new List<Response>();
//...
repo.Setup(r => r.GetProjectConfigurationById(It.IsAny<int>()))
.Returns<int>(id => responses.SingleOrDefault(r => r.Id == id));
Upvotes: 3