简体   繁体   中英

How to convert expression signature to be applicable with `It.IsAny` of `Moq`

Following this answer I've tried to something similar and mock an interface of:

public interface IGetRepository<TEntity>
{
    IEnumerable<TEntity> Get(
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null);
}

This is my unit test method:

IEnumerable<EventModel> expectedList = return new List<MyModel>() 
{
    new MyModel()
    {
        Id = 0
    }
}

using (var _mock = AutoMock.GetLoose())
{
    var repositoryMock = _mock.Mock<IGenericRepository<EventModel>>();
    repositoryMock
        .Setup(items => items.Get(It.IsAny<Expression<Func<EventModel, bool>>>))
        .Returns(() => expectedList);
}

But the It.IsAny<Expression<Func<EventModel, bool>>> create an error message of:

cannot convert from 'method group' to 'Expression>'

Already read these similar questions: 1 , 2 , 3 , so I guess the my issue is different signature, if so how do I convert this signature to be applicable with It.IsAny of moq ?

Please, try this

var expectedList = new List<EventModel>()
            {
                new EventModel()
                {
                    Id = 0
                }
            };

            {
                var repositoryMock = new Mock<IGetRepository<EventModel>>();
                repositoryMock
                    .Setup(items => items.Get(It.IsAny<Func<IQueryable<EventModel>, IOrderedQueryable<EventModel>>>()))
                    .Returns(() => expectedList);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM