I want to mock this interface using Moq
IInterfaceToBeMocked {
IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter);
}
I was thinking of doing something like
_mock.Setup(method => method.SearchDudeByFilter( x=> x.DudeId.Equals(10) && X.Ride.Equals("Harley"))). Returns(_dudes);// _dudes is an in-memory list of dudes.
When I try to debug the unit test where i need this mocking, it says that “expression is not allowed” pointing towards the lambda. If it makes any difference I am using xUnit as the testing framework.
The following works fine for me with the Moq 4.0 Beta:
and the unit test:
Now if you change something into the argument of actual method call the test will no longer pass because the mocked method will return the expected result only if the argument is the same:
Remark: mocking methods that take lambda expressions is a new feature that was not available in previous versions where we need to use
It.Is<SomeType>andIt.IsAny<SomeType>parameter constraints.