具有精确表达式的Moq <func <tentity,bool =“”>&gt;

时间:2016-12-21 15:39:35

标签: c# linq unit-testing moq

我正在尝试测试存储库的Get方法。签名如下:

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

通常以下列方式调用此方法:

Department targetDepartment =
            _departmentRepository.Get(department => department.Id == departmentId).FirstOrDefault();

它可以很好地处理实际数据,当我尝试设置此方法以返回特定的部门时,它会正常运行。用特定的lambda来解决问题。

到目前为止,我已经尝试了很多方法来模拟它,其中一些是:

1)

Expression<Func<Department, bool>> filterExpression2 = d => d.Id == DepartmentId;

_departmentRepositoryMock.Setup(repo => repo.Get(
    It.Is<Expression<Func<Department, bool>>>(y => filterExpression2.Compile()(firstDepartment)),
    null, 
    It.IsAny<string>()))
                         .Returns(new List<Department>() { firstDepartment }.AsQueryable());

2)

Expression<Func<Department, bool>> filterExpression2 = d => d.Id == DepartmentId;

_departmentRepositoryMock.Setup(repo => repo.Get(
    It.IsAny<Expression<Func<Department, bool>>>(),
    null, 
    It.IsAny<string>()))
                         .Returns(new List<Department>() { firstDepartment }.Where(filterExpression2.Compile()).AsQueryable());

我看到很多类似的问题,但不知怎的,它们都没有用。

我要做的是强制模拟的存储库返回特定的部门,具体取决于&#39; Id&#39;参数发送到lambda。

我知道很多使用It.IsAny的回复,但它不是我想要的,因为它会触发传递给Get方法的任何表达式的匹配。

任何人都可以帮助我,或者至少让我朝着正确的方向前进吗?

1 个答案:

答案 0 :(得分:5)

尝试了以下最小例子并且能够通过测试。注意设置和返回。

[TestClass]
public class UnitTest11 {
    [TestMethod]
    public void TestMethod1() {
        //Arrange
        //fake data
        var list = Enumerable.Range(1, 10).Select(id => new Department { Id = id }).ToList();

        var mock = new Mock<IRepository<Department>>();

        mock
            .Setup(repo => repo.Get(
                It.IsAny<Expression<Func<Department, bool>>>(),
                null,
                It.IsAny<string>())
            )
            .Returns(
                (
                    Expression<Func<Department, bool>> filter,
                    Func<IQueryable<Department>, IOrderedQueryable<Department>> orderBy,
                    string includeProperties
                ) => {
                    var func = filter.Compile();
                    var result = list.Where(func);

                    if (orderBy != null) {
                        result = orderBy(result.AsQueryable());
                    }

                    return result;
                }
            );

        var sut = new MyClass(mock.Object);

        var departmentId = 2;

        //Act
        var actual = sut.GetDepartment(departmentId);

        //Assert
        Assert.IsNotNull(actual);
        Assert.AreEqual(actual, list[departmentId - 1]);

    }


    public class MyClass {
        private readonly IRepository<Department> _departmentRepository;

        public MyClass(IRepository<Department> repository) {
            this._departmentRepository = repository;
        }

        public Department GetDepartment(int departmentId) {
            Department targetDepartment = _departmentRepository.Get(department => department.Id == departmentId).FirstOrDefault();

            return targetDepartment;
        }
    }

    public class Department {
        public int Id { get; set; }
    }

    public interface IRepository<TEntity> {
        IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = ""
        );
    }
}