I have the following Interface that is used to DI and IOC webservice clients
public interface IWcfServiceClientProvider <TContract>: IDisposable where TContract: class
{
TResult Execute<TResult>(Expression<Func<TContract, TResult>> expression);
TResult Execute<TResult>(Expression<Func<TContract, TResult>> expression, bool closeConnection = true);
void Execute(Expression<Action<TContract>> expression);
void Execute(Expression<Action<TContract>> expression, bool closeConnection = true);
}
In my test class I have the following:
List<BaseLookup> myList = new List<BaseLookup> {
new BaseLookup { Id =1, Code = "1001"},
new BaseLookup { Id =2, Code = "1002"},
new BaseLookup { Id =3, Code = "1003"}};
In my test method
Mock<IWcfServiceClientProvider<ILookupService>> lookupServiceClinetProvider = new Mock<IWcfServiceClientProvider<ILookupService>>();
var controller = new ElectorSearchController(lookupServiceClinetProvider.Object);
lookupServiceClinetProvider.Setup(mock => mock.Execute(lookup => lookup.GetList(10))).Returns(myList).Verifiable();
var list = controller.testMethod();
lookupServiceClinetProvider.VerifyAll();
list will only have value when the parameter for GetList
is set to 10 i.e GetList(10)
Not GetList(i)
where i=10
The following works
lookupServiceClinetProvider.Setup(mock => mock.Execute(It.IsAny<Expression<Func<ILookupService, List<BaseLookup>>>>(), true )).Returns((List<BaseLookup>)myList).Verifiable();
But I want to mock the call for GetList
and not any call to Execute. If that works, then I can filter the values in the Return
method