我是针对 ADO .NET Entity Framework 编写的单元测试代码。我想用行填充内存数据库,并确保我的代码正确检索它们。
我可以使用 Rhino Mocks 模拟实体框架,但这还不够。我会告诉查询返回给我的实体。这既不会测试 where 子句也不会测试 .Include() 语句。我想确保我的 where 子句只匹配我想要的行,而不匹配其他行。我想确定我已经要求了我需要的实体,而没有我不需要的实体。
例如:
class CustomerService
{
ObjectQuery<Customer> _customerSource;
public CustomerService(ObjectQuery<Customer> customerSource)
{
_customerSource = customerSource;
}
public Customer GetCustomerById(int customerId)
{
var customers = from c in _customerSource.Include("Order")
where c.CustomerID == customerId
select c;
return customers.FirstOrDefault();
}
}
如果我模拟 ObjectQuery 以返回填充了订单的已知客户,我怎么知道 CustomerService 具有正确的 where 子句和 Include?我宁愿插入一些客户行和一些订单行,然后断言选择了正确的客户并填充了订单。