我正在对针对ADO .NET Entity Framework编写的代码进行单元测试。我想使用行填充内存数据库,并确保我的代码正确地检索它们。
我可以使用Rhino Mocks模拟Entity Framework,但这还不够。我会告诉查询返回哪些实体给我。这既不能测试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有正确的条件和Include?我宁愿插入一些客户行和一些订单行,然后断言选择了正确的客户并且订单已经被填充。