我的存储库单元测试存在一些问题,尤其是方法FindBy()
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
{
return Entities.Where(predicate);
}
实体是IDbSet<T> Entities = _context.Set<T>();
我试图在控制器中测试该方法,但我始终在NULL
中获得houses
。
你能给我一些建议吗?
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);
}
这是我的测试方法
[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());
}
答案 0 :(得分:6)
这不是使用It.IsAny的正确方法,它用于替换整个参数,而不是一个参数的一部分 - 在这种情况下,您可以使用它(某些东西)来表示未指定的谓词:
mock.Setup(m => m.FindBy(It.IsAny<Expression<Func<int, bool>>>())
如您所知,控制器方法将使用当前年份,请将其用于设置:
mock.Setup(m => m.FindBy(f => f.Year == DateTime.Now.Year))
然后,如果意外更改控制器方法以使用错误的值,则测试将失败。