单位测试时我应该嘲笑EF的实体吗? [英] Should I mock the entities of EF when unit testing?

查看:163
本文介绍了单位测试时我应该嘲笑EF的实体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了关于嘲弄实体框架(EF)的这篇文章

我们不应该抽象实体的类型吗?为了保持数据访问层(DAL)和业务层(BL)之间的解耦?

Shouldn't we abstract the entities' types as well? In order to preserve decoupling between the Data Access Layer (DAL) and the Business Layer (BL)?

在上面的帖子中,他使用了EF具体生成的实体类型: / p>

In the above post, he used EF concrete generated entities types:

[TestMethod]
public void GetCustomer()
{
    ContextContainerMock container = new ContextContainerMock();
    IMyEntities en = container.Current;

**Customer c = new Customer { ID = 1, FirstName = "John", LastName = "Doe" };**
    en.Customers.AddObject(c);

    CustomerService service = new CustomerService(container);
    var a = service.GetCustomer(1);

    Assert.AreEqual(c.FirstName, a.FirstName);
    Assert.AreEqual(c.LastName, a.LastName);
}


推荐答案

模拟这些。我直接创建,测试和清理。这有助于我解决处理数据库时更加真实世界的问题。模拟测试对于您可能无法访问资源(如DB)的集成而言非常棒。如果这是你的情况,你可能别无选择。希望有帮助。

Personally, I don't mock these. I create, test and cleanup directly. This has helped me catch problem that are more real world scenarios when dealing with the database. Mocking is fantastic for testing integrations where you may not have access to a resource like a DB. If this is your case, then you may have no choice. Hope that helps.

这篇关于单位测试时我应该嘲笑EF的实体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆