How should I test a simple CRUD class?

I'm trying to do very simple things with unit testing in VS2008 to get started and feel it. I think I have testing small data without a database, but now I want to start testing my real solutions, which are almost always CRUD-heavy.

So, suppose I have a class at the data access level that makes standard CRUD material for the Product. I need a test for each of the methods on the Product.

Below I can come up with no real knowledge on this. Is this a way to do this, or ... How do I approach this? Cool (but simple, please) links are also very welcome.

Create

  • Provide some relevant parameters (product name, etc.)
  • Make sure the identifier is returned.
  • Remove product (for cleaning).

Read

  • Create new product
  • Call the select method
  • Make sure the product name matches the name I gave it when creating
  • Delete product

Update

  • Create a new product.
  • Refresh some fields on it
  • Choose a product
  • Make sure some fields match
  • Remove hte product

Delete

  • Create a new product, save ProductID
  • Remove product (Cleaning passage 4!)
  • Check if the product with this product is in the table?

EDIT:

... Or just create a single tag that checks all these things?

+6
unit-testing visual-studio
source share
2 answers

I have been on this path, and here are all the problems that you will encounter:

1) This works well for one record, but what happens when you need 4 other records to create this record? As a result, you create 4 records to test the insertion of one record. This causes all of the following problems.

2) Creating and deleting 4-5 records per test is slow, it will slowly add up and it will take 45 minutes to complete your tests (believe me, I was there). Slow tests mean that you will never run them, which means that they will be broken most of the time and useless.

3) Your deletion will not succeed for any missing foreign key relationship or dependency, and then the data from the garbage will remain in your database. This garbage data causes other failed tests.

In light of this, I beg you to consider two things. First, try using ORM instead of writing all this logic yourself. Then you only need to check your mapping files (or even less depending on the ORM you use) or look at the mockery so that you can isolate the logic of the data access code from direct access to the database.

+5
source share

I believe that it is best to do what you have outlined. However, instead of breaking the creation event into each operation, I try to create one record in one method, but store the id value of this record in a private class variable, and then return it (read) inside each method. So then I have the Read assert method, then another confirmation confirmation method, and finally the Delete assert method.

Although, you described above, very carefully. However, the biggest problem is that if your deletion method does not work in your read assert method, you can be misleading in thinking that your read functions are violated when the deletion functionality is actually removed.

0
source share

All Articles