下面是我运行良好的测试用例。
[TestMethod]
public void GetCompanies_WhenInvokedWithSearchText_ShouldReturnFilteredCompanies()
{
// Arrange
var context = new Mock<IDataContext>(MockBehavior.Strict);
var companies = new List<Company>
{
new Company()
{
Address = "London",
Name = "ABC Inc."
},
new Company()
{
Address = "Newyork",
Name = "Toyota"
},
new Company()
{
Address = "Ealing broadway",
Name = "Amazon"
}
};
context.Setup(s => s.Query<Company>()).Returns(companies.AsQueryable());
var repository = new CompanyRepository(context.Object);
// Act
var expectedCompanies = repository.GetCompanies("ABC");
// Assert
Assert.AreEqual(1, expectedCompanies.Count);
Assert.AreEqual("London", expectedCompanies.ToList()[0].Address);
}
我的存储库代码是这样的:
public ICollection<Company> GetCompanies(string searchText)
{
Guard.ArgumentNotNull(searchText, "searchText");
return _dbContext.Query<Company>().Where(c => c.Name.Contains(searchText) || c.Address.Contains(searchText)).ToList();
}
我只是不知道最小起订量是如何应用实际方法中存在的过滤器(在哪里),但我没有在测试中设置?
我的猜测是,当测试执行模拟对象的 Query 方法时,已经应用了过滤器来调用它。是否发现使用反射动态存在 where 子句?
只是想清楚地理解它。