unit testing - Database interaction code -


I am writing a small application that works with a database and how can I check this interaction. My application depends on 5 stored procedures, which I have decided to incubate in one class. This class offers 5 public methods that execute processes and, if necessary, converts the results into an object collection.

I really can not understand how to test my code in a simple way and I think that I probably made some mistakes.

What do you think about my design? Is there a better way to handle these matters?

Thank you very much for your help

EDIT: There are only "Insert" and "Selection" questions in my stored procedures. Generally, I recommend for testing things against a live database for some reasons:

< P>>
  1. SQL can execute your tests may take a long time
  2. Data may change under your nose, resulting in broken test not even a visible code change You can. You want your tests to be different and deterministic, so they pass or fail, when your code changes.
  3. If you have a code that updates a database, then you need to roll your changes back or your next test result is in a false positive or false negative.

So to test, you want to duplicate your database. Start with something like this:

  Public Interface IDataRepository {Customer GetCustomerByName (name of string); Zero save customer (customer c); SecurityToken login (string username, string password); } Class database repository: IDataRepository {...} // invites your stored procedures  

If your classes need anything from the database, then the Pass IDataRepository .

The good thing about this setup is that you can create FakeDataRepository to apply the same interface - it does not ask for anything, only data with hard code Gives you the exception case for a happy case, and can return data for other needs.

In other words, you are not testing the interaction between your classes and databases - point-to-point testing of a unit is to test a piece of functionality regardless of other running parts in your app.


Now if you can need to test your stored procedures, then you can directly test for your database registritor class write.

After each test you have to set your database again in its original state - so that you can either run everything in the transaction and rollback, or if you run each test, a new database with scratch data Make them. I like the latter approach - it's easy to forget to leave a transaction or roll it back, and so destroy all your test data.

In this way, databases can take an arbitrary long time to execute the test, so you are probably making a different project strictly for testing your database (especially if you have something There are hundred stored procedures).

I think about this as a system or integration test, not a unit test.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -