【发布时间】:2010-10-09 04:17:57
【问题描述】:
我是针对 ADO .NET 实体框架编写的单元测试代码。我想用行填充内存数据库,并确保我的代码正确检索它们。
我可以使用 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?我宁愿插入一些客户行和一些订单行,然后断言选择了正确的客户并填充了订单。
【问题讨论】:
-
就像你最终做的那样,我使用接口来遵循存储库模式和工作单元模式。然后,我有两个命名空间-> EF 和 Fake。在我的 Fake 存储库中,我只是使用 IList
来存储我的东西并利用 Linq to Objects 来提取数据。效果很好:) -
EntityFramework 7 现在有 InMemoery 提供程序。评论时仍处于测试阶段,但如果您订阅每晚的 nuget,您可以获得它。
标签: entity-framework rhino-mocks