I have a method on my data access layer that can take any function as search criteria and run this against our Entity Framework entities. I am trying to create unit tests using Rhino Mocks on the business layer, but this calls the DAL method. When I try to create a stub for this search method, I cannot seem to get it to run correctly. I have the following method that needs to be stubbed:
IQueryable<T> AllSearchBy<T>(params Expression<Func<T, bool>>[] search) where T : class;
I cannot seem to find a generic expression to use such as Arg.Is.Anything
for functions, so I tried setting up my own. I have the following that should return the first value in my _fakeObjs if the Id is 1 and should return a null if the Id is 0 (two separate tests):
myObjId = 1; // or 0 for returning a null
System.Linq.Expressions.Expression<Func<MyObj, bool>> myFunc = (a => a.Id == objId);
IRepository repositoryMock = MockRepository.GenerateMock<IRepository>();
repositoryMock.Stub(x => x.AllSearchBy<MyObj>(myFunc)).Return(_fakeObjs.Where(x => x.Id == myObjId));
However, I am getting the following errors. For the one that should return an object (value = 1):
Message: Test method
NS.MyApp.Test.ObjTest.SearchById_ReturnsObj threw exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: source
For the one that should return a null (value = 0):
Message: Test method
NS.MyApp.Test.ObjTest.SearchById_ReturnsNull threw exception:
Rhino.Mocks.Exceptions.ExpectationViolationException:
IRepository.AllSearchBy<NS.EF.MyObj>([]); Expected #1, Actual #0.
What do I need to do to set up a parameter to pass into the AllSearchBy in my repository?
Thanks in advance for any help!!
What if you try to pass following as an argument instead of current myFunc
:
Arg<Expression<Func<MyObj, bool>>[]>.Is.Anything