Moq return with multiple Linq expressions

I have the following method in the repository that I am trying to execute Mock:

IEnumerable<TEntity> GetAll(
      Expression<Func<TEntity, bool>> filter = null,
      Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
      string includeProperties = "")

I installed the following:

mockContactNumberRepository.Setup(x => x.GetAll(
    It.IsAny<Expression<Func<ContactNumber, bool>>>(), 
    It.IsAny<Func<IQueryable<ContactNumber>, IOrderedQueryable<ContactNumber>>>(),
    It.IsAny<string>()))
    .Returns(new Func<Expression<Func<ContactNumber, bool>>, 
        IQueryable<ContactNumber>>(ex => _contactNumbers.Where(ex.Compile()).AsQueryable()));

When starting unit test, an error message appears displaying a mismatch between the parameter counter. I understand this because Returnsonly the first parameter indicates, but I'm not sure how to specify additional parameters.

I found many questions that ask similar questions, but did not find one with several lambda expressions.

Any help you can give would be greatly appreciated.

+1
source share
1 answer

GetAll IEnumerable<TEntity>. valueFunction Returns . valueFunction , , GetAll. ( , , , ):

mockContactNumberRepository
.Setup(x => 
    x
    .GetAll(
        It.IsAny<Expression<Func<ContactNumber, bool>>>(), 
        It.IsAny<Func<IQueryable<ContactNumber>, IOrderedQueryable<ContactNumber>>>(),
        It.IsAny<string>()))
.Returns(new Func<
    Expression<Func<ContactNumber, bool>>, 
    Func<IQueryable<ContactNumber>, IOrderedQueryable<ContactNumber>>,
    string,
    IEnumerable<TEntity>>((arg1, arg2, arg3) => 
        {
            // arg1 is Expression<Func<ContactNumber, bool>>
            // arg2 is Func<IQueryable<ContactNumber>, IOrderedQueryable<ContactNumber>>
            // arg3 is string
            // Do something here and return an IEnumerable<TEntity>
        }));
+1

Source: https://habr.com/ru/post/1014181/


All Articles