我的 UnitTest 应该模拟我的 UnitOfWork、存储库和上下文还是仅模拟上下文?
我对单元测试有点陌生,所以希望这个问题有意义。
我的设置: 视觉工作室2010 实体框架4.1 起订量
我的 BAL 中有一个服务类,它使用 DAL 中的 UnitOfWork。 UnitOfWork 管理对各种存储库的访问,而这些存储库又通过 Context 对象访问数据库。
我想为服务类公共方法创建一个单元测试,该方法负责利用 lambda 表达式处理一些非常复杂的“GetNextObject”类型逻辑。
问题: 我可以很容易地模拟我的 DBContext 并创建一个我想要测试我的服务方法的对象的 DBSet(服务类本质上查询存储库)。这是执行此操作的正确方法还是这更像是集成测试?通过模拟 Context,我删除了数据库,但我仍然使用 UnitOfWork 和 Repository 类。我应该对所有这些对象进行复杂的模拟吗?
谢谢! 弗里兹
I am somewhat new to unit tests so hopefully this question makes sense.
My Setup:
Visual Studio 2010
Entity Framework 4.1
Moq
I have a Service class located in my BAL which uses the UnitOfWork in my DAL. The UnitOfWork manages access to various repositories which in turn access the database through a Context object.
I would like to create a unit test for a service class public method which is responsible for some very complicated "GetNextObject" type logic utilizing lambda expressions.
Question:
I can very easily Mock my DBContext and create a DBSet of objects that I want to test my Services method against(the service class essentially queries the repository). Is this the correct way to do this or is this more of an integration test? By mocking the Context I have removed the database, but I still am using the UnitOfWork and Repository classes. Should I instead do a complicated mocking of all these objects?
Thanks!
AFrieze
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的服务仅依赖于存储库和工作单元,那么您不必模拟 DbConext,不是吗?一般来说,我们不模拟 DbContext 主要是因为没有太多需要测试的内容,因为存储库/unitofwork 基本上是 dbcontext 的包装器。为了测试您的服务,模拟存储库和工作单元应该足够了。如果您想测试数据库操作,请进行集成测试。
Assuming your service only depends on repository and unitofwork, you don't have to mock DbConext, do you? Generally, we don't mock DbContext mainly because there is not much to test since repository/unitofwork are basically wrappers of dbcontext. In order to test your services, mocking repository and unitofwork should be enough. If you want to test DB operations, do the integration test.