简体   繁体   中英

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.

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.

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.

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

Also Pluralsight has some courses on this. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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