Unit Testing – Database Coupled App

unit testing

What would be the best approach at unit testing a model that integrates into an application that is tightly coupled to database?

The specific scenario here is a shopping cart – I'd like to be able to test the adding removing and retrieving of items from the cart as well as pricing logic etc. This in my mind all requires database access though I have read several times that database access should be avoided.

Best Answer

Dependency injection is one way of handling this. You can set-up a test database to mimic the shopping cart, or you can even write some code that "confirms" the customer's transaction. Then at runtime, your software will pick which component to connect to.

Just don't connect to the production database for anything during testing!