简体   繁体   English

数据访问层c#的单元测试

[英]unit testing for Data access layer c#

I have the following method wherein the business layer is interacting with the data access layer and returning the collection object.我有以下方法,其中业务层与数据访问层交互并返回集合对象。 I'm new to unit testing, but need to add automated unit tests to the solution.我是单元测试的新手,但需要向解决方案添加自动化单元测试。 I read multiple articles and theory related to unit testing, but I'm confused with how to proceed.我阅读了多篇与单元测试相关的文章和理论,但我对如何进行感到困惑。 It would be really helpful If somebody can guide me with approach,如果有人可以指导我的方法,那将非常有帮助,

[DataObjectMethod(DataObjectMethodType.Select, true)]
public static WorkQueueBE GetItemByDetailsID(int detailsID)
{  return WorkQueueDB.GetItemByDetailsID(detailsID); }

This method gives call to GetItemsByDetailsID method in db layer, which in turn calls a stored procedure, gets the data from database, fills the collection and returns an object.该方法调用 db 层的 GetItemsByDetailsID 方法,该方法依次调用存储过程,从数据库中获取数据,填充集合并返回一个对象。

I'm gonna summarize the comments a bit as well as add some new thoughts.我将稍微总结一下评论并添加一些新想法。 You write你写

This method gives call to GetItemsByDetailsID method in db layer, which in turn calls a stored procedure, gets the data from database, fills the collection and returns an object.该方法调用 db 层的 GetItemsByDetailsID 方法,该方法依次调用存储过程,从数据库中获取数据,填充集合并返回一个对象。

A comment to this is -> A unit test should only test an isolated part of your logic, that will say a single method.对此的评论是 -> 单元测试应该只测试逻辑的一个孤立部分,即一个方法。 Not the entire flow, that's an integration test.不是整个流程,这是一个集成测试。

From what I see in your code snippet you use concrete classes.从我在您的代码片段中看到的内容来看,您使用了具体的类。 If you really want to make your application easy to test you need to use interfaces and abstract classes that can be instantiated to concrete classes as well as easily mocked and stubbed.如果您真的想让您的应用程序易于测试,您需要使用可以实例化为具体类以及易于模拟和存根的接口和抽象类。 An natural way on how to learn how to implement interfaces, abstract classes and concrete classes is to do Test Driven Development.学习如何实现接口、抽象类和具体类的一种自然方式是进行测试驱动开发。 Start with a small project and learn from there :)从一个小项目开始并从中学习:)

If I would want to unit test your method that you've provided I would separate your logic from the data-access layer.如果我想对您提供的方法进行单元测试,我会将您的逻辑与数据访问层分开。 This I would do by making the data-access layer classes implement interfaces of what they should do.我会通过让数据访问层类实现它们应该做什么的接口来做到这一点。 This way I can mock the data-access layer and just return a specific snippet of data, just the part I need to create my unit tests for the business-layer method.通过这种方式,我可以模拟数据访问层并只返回特定的数据片段,这只是我需要为业务层方法创建单元测试的部分。 After all, in this case I want to test the business-layer-method's logic, not the data-access-layer-method's.毕竟,在这种情况下,我想测试业务层方法的逻辑,而不是数据访问层方法的逻辑。

It is quite tough to start doing unit-testing-friendly code but when you start getting a grip of you are gonna love it :)开始编写对单元测试友好的代码非常困难,但是当您开始掌握它时,您会爱上它:)

This was a lot of theory and no concrete example because I think you need to start with a small project of your own and do it the TDD way, by doing this you will understand how everything works concerning unit testing.这是很多理论,没有具体的例子,因为我认为你需要从你自己的一个小项目开始,并以 TDD 的方式来做,通过这样做,你将了解关于单元测试的一切是如何工作的。

Some links to get you startedhttps://msdn.microsoft.com/en-us/library/aa730844(v=vs.80).aspx https://msdn.microsoft.com/en-us/library/ff847525(v=vs.100).aspx http://www.codeproject.com/Articles/321154/Test-Driven-Development-TDD-in-Csharp一些帮助您入门的链接https://msdn.microsoft.com/en-us/library/aa730844(v=vs.80).aspx https://msdn.microsoft.com/en-us/library/ff847525( v=vs.100).aspx http://www.codeproject.com/Articles/321154/Test-Driven-Development-TDD-in-Csharp

Also Pluralsight has some courses on this. Pluralsight 也有一些这方面的课程。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM