我正在尝试测试存储库的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的特定“部门”时,问题就开始了。
到目前为止,我试过很多方法来嘲笑它,下面是其中的一些:
(一)
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());

我看到许多类似的问题,但不知怎么的,没有一个是有用的。
我要做的是根据发送给lambda的“id”参数,强制模拟存储库返回特定的部门。
我知道许多响应使用It.IsAny,但这不是我想要的,因为它会触发传递到get方法的任何表达式的匹配。
有谁能帮我,或者至少把我推向正确的方向吗?

最佳答案

尝试了下面的最小示例,并通过了测试。注意设置和返回。

[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 = ""
        );
    }
}

09-06 02:19