Mario Guadanhim Mario Guadanhim - 8 days ago 5
C# Question

Mocking Generic Repository unit test

I'm creating a unit test and i'm using rhino mocks (i've never worked with any kind of mocks before, (if you guys know how to handle it with another mock framework, please show me some examples with them)).

In my scenario i'm trying to mock the following generic repository

public interface Repository
{
IEnumerable<TEntity> Search(Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> order = null);
}


My unit test:

_classRepository.Stub(x => x.Search(myclass =>
myclass.id == 1 &&
).Return(arrayClass.AsQueryable());


the array class has only one register with id 1.

When i run my test i receive the following message:


Value cannot be null.
Parameter name: source

Answer

I solved my problem when changed my mock framework to Moq. So i could use the lambda expressions on my mock setup.

_myclassRepository.Setup(x => x.Search(
          myclass => myclass.id == 1,
          It.IsAny<Func<IQueryable<MyClass>, IOrderedQueryable<MyClass>>>(),
          It.IsAny<string>(),
          It.IsAny<int>())).Returns(arrMyClass);