Below is my test case which runs fine.
下面是我运行良好的测试用例。
[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);
}
My repository code is like this:
我的存储库代码是这样的:
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();
}
I just do not get exactly how Moq happen to apply the filter (where) which is present on the actual method but I did not set up in the test?
我只是不知道Moq是如何应用实际方法上的过滤器(在哪里)的,但是我在测试中没有设置?
My guess is, when test executes mocked object's Query method is called with the filter applied to it already. Is it discovering a where clause is present dynamically using reflection?
我的猜测是,当测试执行时,使用已经应用到对象上的过滤器来调用对象的查询方法。它是否发现了使用反射动态显示的where子句?
Just want to understand it clearly.
只是想弄清楚。
1 个解决方案
#1
2
There is no magic :) Take a look to this line
没有魔法:)看看这句台词
context.Setup(s => s.Query<Company>()).Returns(companies.AsQueryable());
When method Query<Company>()
is executed, returns companies.AsQueryable()
. Where
is executed over this, so Moq is not guessing anything.
当方法查询
#1
2
There is no magic :) Take a look to this line
没有魔法:)看看这句台词
context.Setup(s => s.Query<Company>()).Returns(companies.AsQueryable());
When method Query<Company>()
is executed, returns companies.AsQueryable()
. Where
is executed over this, so Moq is not guessing anything.
当方法查询