WHERE语句在使用It.IsAny的单元测试中不起作用

Rom*_*syk 2 c# linq asp.net unit-testing mocking

我的存储库的单元测试存在一些问题,尤其是方法 FindBy()

public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
{
   return Entities.Where(predicate);
}
Run Code Online (Sandbox Code Playgroud)

实体是 IDbSet<T> Entities = _context.Set<T>();

我想测试方法的控制器,但我总是NULLhouses.
你能给我一些建议吗?

public override HttpResponseMessage Get()
{
    var houses = Repository.FindBy(y => y.Year == DateTime.Now.Year);

    if (houses != null && houses.Any())
    {
        return Request.CreateResponse(HttpStatusCode.OK, houses);
    }

    const string message = "House: No content";
    return ErrorMsg(HttpStatusCode.NotFound, message);
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试方法

[TestMethod]
public void GetAll()
{   //Arrange
    var houses = new List<House>
     {
         new House {ID = 1, BuildNr = "01", HouseNr = "001" },
         new House {ID = 2, BuildNr = "02", HouseNr = "002" },
         new House {ID = 3, BuildNr = "03", HouseNr = "003" },
         new House {ID = 4, BuildNr = "04", HouseNr = "004" },
         new House {ID = 5, BuildNr = "05", HouseNr = "005" }
     };

    var mock = new Mock<IRepository<T>>();
    mock.Setup(m => m.FindBy(f => f.Year == It.IsAny<int>()))
        .Returns(houses.AsQueryable());
    //...

    var controller = new HouseController(mock.Object);
    //Action
    var response = controller.Get();
    var result = response.ContentToQueryable<T>();
    //Assert
    Assert.AreEqual(5, result.Count());
}
Run Code Online (Sandbox Code Playgroud)

stu*_*rtd 6

这不是使用It.IsAny的正确方法,它用于替换整个参数,而不是一个参数的一部分 - 在这种情况下,您可以使用它(某些东西)来表示未指定的谓词:

 mock.Setup(m => m.FindBy(It.IsAny<Expression<Func<int, bool>>>())
Run Code Online (Sandbox Code Playgroud)

您已经知道控制器方法将使用当前年份,请将其用于设置:

mock.Setup(m => m.FindBy(f => f.Year == DateTime.Now.Year))
Run Code Online (Sandbox Code Playgroud)

然后,如果意外更改控制器方法以使用错误的值,则测试将失败.

  • @Rob我的一位同事试图通过使用"TimeProvider"类来缓解单元测试中的日期问题.不幸的是,他把它变成了一个静态类,所以一旦测试开始同时运行,它们就会踩到彼此的预期时间值.娱乐时间 :) (2认同)