I am writing a little application that actually works having a database and that i can't figure how you can test this interaction. My application depends on 5 saved methods which i have made the decision to encapsulate these questions class. These kinds offers 5 public techniques that execute the methods and, at the appropriate interval, convert the outcomes into collections of objects.

I truly can't learn how to test my code inside a simple way and i believe that most likely I made some mistakes.

Exactly what do you consider my design? It is possible to better method to handle these cases?

Thanks greatly for the help

EDIT: Among my saved methods you will find only "place" and "choose" queries.

Why don't you just call each one of the techniques, which in turns, calls the saved methods and look at the outcomes by iterating with the collections? If you will find many results, limit the amount that return for testing reasons.

Now if you're attempting to verify the standard from the data, I do not think this really is the proper way to get it done.

I frequently employ automated integration testing when confronted with databases and logic therein. The overall idea is to setup an evaluation database while using schema for the application. Within the test script, you are able to populate the exam database. If you have everything setup, make use of your class using the test database to check that things are working OK. Affect the data and test again.

You will need to be certain to create and destroy the exam database throughout each test run. You wouldn't want data from previous tests, otherwise you can't precisely test the saved methods.

Since you will find more gamers in integration testing than you will find in unit testing, you will need to do more setup and cleanup work.

You can test out your class using unit tests, mocking the database connection, but that will not verify the saved methods will work correctly. Rather than writing unit tests for saved methods, it's frequently simpler to simply do integration tests.

Generally, I'd recommend against testing things against an active database for any couple of reasons:

  1. Performing SQL could make your results have a very long time
  2. Data can alter beneath your nose, leading to damaged tests despite no visible code changes. You would like your results to become isolated and deterministic, so that they pass or fail only if your code changes.
  3. For those who have code which updates a database, you have to roll back your changes or perhaps your next test run could cause an incorrect positive or false negative.

So for testing, you need to fake your database. Begin with something similar to this:

public interface IDataRepository {
    Customer GetCustomerByName(string name);
    void SaveCustomer(Customer c);
    SecurityToken Login(string username, string password);
}

class DatabaseRepository : IDataRepository { ... } // invokes your stored procedures

In case your classes need something in the database, pass an IDataRepository to your object's constructor.

Nice factor relating to this setup is the best way to produce a FakeDataRepository applying exactly the same interface -- it does not invoke anything, only returns hard-coded data. You are able to return data for that happy situation, for that exception situation, along with other needs.

Quite simply, you are not testing the interaction involving the classes and database whatsoever -- the purpose of one test would be to test just one bit of functionality without caring concerning the other moving parts inside your application.


If you need to check your saved methods, you need to can write tests for the DatabaseRepository class directly.

You have to re-set your database to the original condition after each test -- which means you either run my way through a transaction and rollback, or else you produce a new database with scratch data on each test run. I favor the second approach -- its too simple to leave a transaction open or forget to roll it back, and for that reason destroy all your test data.

Database tests such as this may take an randomly very long time to complete, so you are most likely best developing a separate project strictly for testing your database (particularly if you possess a couple of hundred saved methods).

I'd think about this weight loss a method or integration test, not really a unit test.