Components without services as a test framework for data access object testing

Visual Studio 2005 includes a set of nice tools for software quality assurance, but not all this tools are magic, especially when we are looking for data access object tests. Usually these kinds of objects are the data access abstraction layer for any tiered application.

One of the biggest problems in data access object testing, are the unstable state of the underling data source and a lot of solutions emerge to fix this problem, like , mock objects, database transactions, compensation transactions, and so on. Plainly all those solutions require a little beat of effort and on some cases there is no possible to create a test case.

Hopefully COM+ technology in the version 1.5 presents a new kind of enterprise service called Services without Components, where you can use the services provided by COM+ without needing to build a ServicedComponent. By using COM+ services without components, we can avoid the overhead of creating a component that is used to access only the COM+ services we need to build our DAL unit testing.

The Unit Testing of Microsoft Quality Tools from Visual Studio 2005 provides a great framework from our integrated test development environment. It arrives with a set of custom method attributes that permit us to introduce some code at the initialization of the test, the test itself and at the end of test to cleanup the logic applied to the test. Obviously the idea was to provide a test framework that provides us the option to hook COM+ 1.5 services for our convenience.

The following code shows how easy is to enlist the component on a distributed transaction coordinated by the DTC. At test cleanup we can abort the transaction to leave the data source in the same state prior to test execution.

[TestClass]

public class TransactionalTest

{

    [TestInitialize]

    public void Initialize()

    {

        ServiceConfig config = new ServiceConfig();

        config.Transaction = TransactionOption.RequiresNew;

        ServiceDomain.Enter(config);

    }

    [TestMethod]

    public void DatabaseOperationTest()

    {

        // Some CRUD operation

    }

    [TestCleanup]

    public void Cleanup()

    {

        if (ContextUtil.IsInTransaction)

        {

            ContextUtil.SetAbort();

        }

        ServiceDomain.Leave();

    }

}

I hope this brief explication can help on your test driven developments.

Happy coding!