3

I have methods like this:

public static bool ChangeCaseEstimatedHours(int caseID, decimal? time)
   {
       Case c = Cases.Get(caseID);

       if (c != null)
       {
           c.EstimatedHours = time;
           return Cases.Update(c);
       }

       return false;
   }

   public static bool RemoveCase(int caseID)
   {
       return Cases.Remove(caseID);
   }

which internally uses LINQ to do the queries.

I am wondering how I should go about testing these. They do not have a state so they are static. They also modify the database.

Thus, I would have to create a case, then remove it in the same test, but a unit test should only be doing 1 thing. What is usually done in these situations?

How do I test database queries, updates and deletes?

Thanks

1
  • 1
    The question is do you need to make it a unit test or are you happy with it being an integration test. If the earlier look at mocking as described by @Ragzitsu. If you're happy with it being an integration test, then you can just use transactions to isolates and rollback changes. Of course test run isolation and integration come with the added headaches of synchronisation, while unit testing comes with the problem of design time isolation using patterns such as described by Radek. Decide what you need first - I recommend stackoverflow.com/questions/509445/… Commented Jan 21, 2013 at 22:36

3 Answers 3

2

A test that requires database access is probably the hardest test to maintain. If you can avoid touching the database in your unit test, I would try to create an interface for the Cases class.

interface ICase
    {
        ICase Get(int caseID);
        bool RemoveCase(int caseID);
    }

And then use RhinoMock, Moq to verify if the Get() and RemoveCase() were called.

If you insist you need to test with a database, then you will need to spend time on setting up a testing database and do proper data clean up.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a difference between "Knowing the path and walking the path". What you want to test would class as an integration test not a unit test. A unit test is something that is carried out in isolation without any external dependencies and therefore makes the test runnable even if the machine you are running it on does not have a physical database instance present on it. See more differences here.

That's why patterns like Repository are really popular when it comes to doing persistence based applications as they promote unit test-ability of the data layer via Mocking. See a sample here.

So, you have 2 options (the blue pill Or the red pill):

Blue Pill: Buy a tool like TypeMock Isolator essential or JustMock and you'll be able to mock anything in your code "and the story ends".

Red Pill: Refactor existing design of the data layer to one of the interface based patterns and use free Mocking frameworks like Moq or RhinoMocks and "see how deep the rabbit hole goes"

All the best.

Comments

1

Maybe you could consider approach based on Rails framework:

  1. Initialize database tables content via presets (fixtures in Rails)
  2. Open transaction
  3. Run test
  4. Rollback transaction
  5. Go to point 2. with another test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.