Search code examples
c#moqrepository-patternexpression-trees

Matching arguments with Moq.ItIs<expression tree>()


I don´t know how to pass a expression tree using the Moq.ItIs<>

The type of the parameter is Expression<Func<Destination, bool>>

But I don´t understand why I can´t pass something like this Moq.ItIs<Expression<Func<Destination, bool>>>(d=>d.Property == "test")

I get the error " Error CS1061 'Expression<Func<Destination, bool>>' does not contain a definition for 'Enable' and no accessible extension method 'Enable' accepting a first argument of type 'Expression<Func<Destination, bool>>' could be found (are you missing a using directive or an assembly reference?) ...\MockRepositories\GenericMock.cs 484 Active "

DestinationRepository
              .Setup(x => x.GetAsync(
It.Is<Expression<Func<Destination, bool>>>(d=>d.Enable), It.IsAny<Func<IQueryable<Destination>,IOrderedQueryable<Destination>>>(),
It.Is<string>(x => x == Constants.IncludeParentDestinationInfo),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<bool>()))
             .Returns(Task.FromResult(DestinationStub.DestinationsParentInfo))
             .Verifiable();

Task<IEnumerable<TEntity>> GetAsync(Expression<Func<TEntity, bool>>? whereCondition = null,
                                            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
                                            string includeProperties = "",
                                            int startRecord = -1,
                                            int pageSize = -1,
                                            bool forceSingleQuery = false);

I don´t know how to pass the expression tree for the first parameter, the whereCondition.

I try create the expression tree before

Expression<Func<Destination, bool>> expr = d => d.Enable;

        DestinationRepository
          .Setup(x => x.GetAsync(It.Is<Expression<Func<Destination, bool>>>(expr), It.IsAny<Func<IQueryable<Destination>,
                                     IOrderedQueryable<Destination>>>(), It.Is<string>(x => x == Constants.IncludeParentDestinationInfo), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<bool>()))
         .Returns(Task.FromResult(DestinationStub.DestinationsParentInfo))
         .Verifiable();

Solution

  • It's a bit unclear what you want to achieve by using It.Is, but I'm assuming you want the mock to only return a value if GetAsync is called specifically with the expression d => d.Enable.

    If so, you can do this without the use of It.Is like this (and similarly for your includeProperties parameter):

    DestinationRepository
        .Setup(x => x.GetAsync(
            d => d.Enable, // <--
            It.IsAny<Func<IQueryable<Destination>, IOrderedQueryable<Destination>>>(),
            Constants.IncludeParentDestinationInfo, // <--
            It.IsAny<int>(),
            It.IsAny<int>(),
            It.IsAny<bool>()
        ))
        .Returns(Task.FromResult(DestinationStub.DestinationsParentInfo))
        .Verifiable();