Click here to Skip to main content
15,886,664 members
Articles / Database Development / SQL Server

How design patterns can help you in developing unit testing-enabled applications

Rate me:
Please Sign up or sign in to vote.
4.61/5 (34 votes)
15 Nov 2007CPOL15 min read 127.3K   323   177  
This article shows how you can mix together Model-View-Presenter, Domain Model, Services, ActiveRecord, and Repository patterns to create a testable application.
using Castle.ActiveRecord;
using NHibernate;
using Northwind.DataAccess;
using Northwind.DataAccess.Repository;
using Northwind.Domain.Model;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace Northwind.DataAccess.Test
{
    [TestFixture]
    public class InMemoryCustomerRepositoryTest
    {
        private Customer customer;
        private InMemoryCustomerRepository customerRepository;

        [SetUp]
        public void Setup()
        {
            customer = new Customer();
            customerRepository = new InMemoryCustomerRepository();
            //We must initialize ActiveRecord db settings before working with ActiveRecords.
            DataAccessHelper.Initialize();
        }

        [Test]
        public void InsertAndGetCustomerTest()
        {
            Customer c = new Customer();
            Assert.AreEqual(true, c.IsNew, "c.IsNew");
            c.ID = "TEST";
            c.CompanyName = "CUSTOMER TEST";
            c.ContactName = "CONTACT TEST";
            using (ISession session = DataAccessHelper.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                customerRepository.AddOrUpdate(session, c);
                session.Transaction.Commit();
            }
            Assert.AreEqual(false, c.IsNew, "c.IsNew");
            Customer c2 = customerRepository.Load("TEST");
            using (ISession session = DataAccessHelper.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                customerRepository.Remove(session, c2);
                session.Transaction.Commit();
            }
        }

        [Test]
        public void InsertAndUpdateCustomerTest()
        {
            Customer c = new Customer();
            c.ID = "TEST";
            c.CompanyName = "CUSTOMER TEST";
            c.ContactName = "CONTACT TEST";
            using (ISession session = DataAccessHelper.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                customerRepository.AddOrUpdate(session, c);
                session.Transaction.Commit();
            }
            Customer c2 = customerRepository.Load("TEST");
            c2.CompanyName = "CUSTOMER TEST UPDATED";
            c2.ContactName = "CONTACT TEST UPDATED";
            using (ISession session = DataAccessHelper.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                customerRepository.AddOrUpdate(session, c2);
                session.Transaction.Commit();
            }
            Customer c3 = customerRepository.Load("TEST");
            Assert.AreEqual("CUSTOMER TEST UPDATED", c3.CompanyName);
            Customer c4 = customerRepository.Load("TEST");
            using (ISession session = DataAccessHelper.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                customerRepository.Remove(session, c4);
                session.Transaction.Commit();
            }
        }

        [Test]
        public void InsertAndDeleteCustomerTest()
        {
            Customer c = new Customer();
            c.ID = "TEST";
            c.CompanyName = "CUSTOMER TEST";
            c.ContactName = "CONTACT TEST";
            using (ISession session = DataAccessHelper.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                customerRepository.AddOrUpdate(session, c);
                session.Transaction.Commit();
            }
            Customer c2 = customerRepository.Load("TEST");
            using (ISession session = DataAccessHelper.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                customerRepository.Remove(session, c2);
                session.Transaction.Commit();
            }
            try
            {
                Customer c3 = customerRepository.Load("TEST");
                Assert.IsTrue(c3 == null, "customer has not been deleted");
            }
            catch
            {
            }
        }

        [TearDown]
        public void TearDown()
        {
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions