We have done it like this:

Create a test fixture base which generates an in-place configuration to be used.
We are using a SQLite in memory setup. This has the advantage of being fast and no cleanup required but has the disadvantage of being different from our production setup which uses a MS SqlServer database (different database engine problems). It also requires to keep the connection open during the whole test as the SQLite in memory database gets killed once you close the connection so we needed to provide a custom connection provider (the SQLiteInMemoryTestingConnectionProvider)

But you could adapt the config below to point to a any database server you like.

The code below was written against Castle 1.0 RC3 so some interfaces might have 
slightly changed but it should still work.


    public class ActiveRecordInMemoryTestFixtureBase
    {
        protected static InPlaceConfigurationSource _Configuration = null;

        public static void OneTimeInitalize(params Assembly[] assemblies)
        {
            if (_Configuration != null) { return; }

            Hashtable properties = new Hashtable();
            properties.Add("hibernate.connection.driver_class", 
"NHibernate.Driver.SQLite20Driver");
            properties.Add("hibernate.dialect", 
"NHibernate.Dialect.SQLiteDialect");
            properties.Add("hibernate.connection.provider",
                typeof(SQLiteInMemoryTestingConnectionProvider).FullName + "," +
                
typeof(SQLiteInMemoryTestingConnectionProvider).Assembly.FullName);
            properties.Add("hibernate.connection.connection_string", "Data 
Source=:memory:;Version=3;New=True;");

            _Configuration = new InPlaceConfigurationSource();
            _Configuration.Add(typeof(ActiveRecordBase), properties);
            ActiveRecordStarter.Initialize(assemblies, _Configuration);
        }

        protected SessionScope _Session;

        protected void CreateSession()
        {
            ActiveRecordStarter.CreateSchema();
            _Session = new SessionScope();
        }

        protected void DisposeSession()
        {
            _Session.Dispose();
            if (SQLiteInMemoryTestingConnectionProvider.Connection != null)
            {
                SQLiteInMemoryTestingConnectionProvider.Connection.Close();
            }
            SQLiteInMemoryTestingConnectionProvider.Connection = null;
        }
    }

    public class SQLiteInMemoryTestingConnectionProvider : 
NHibernate.Connection.DriverConnectionProvider
    {
        public static IDbConnection Connection = null;

        public override IDbConnection GetConnection()
        {
            if (Connection == null)
            {
                Connection = base.GetConnection();
            }

            return Connection;
        }

        public override void CloseConnection(IDbConnection conn)
        {
            // we never automatically close the connection once it is open
            // it has to be closed manually
        }
    }

Example usage with NUnit:

    [TestFixture]
    public class SomeARTest : ActiveRecordInMemoryTestFixtureBase
    {
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            OneTimeInitalize(typeof (SomeModel).Assembly);
        }

        [SetUp]
        public void SetUp()
        {
            CreateSession();
        }

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

hope it helps

Cheers
Christian

Neil wrote:
Hello,

We have an exist application which makes use of ActiveRecord to access
the database (SQL Server 2005).  Going forward we are planning on
adding unit tests to parts of the system to help reduce manual testing
time.

One of the areas of we are unsure on is how we should be testing
classes that depend directly on the database without some major
refactoring to introduce interfaces for all our ActiveRecord classes.
Does any one have some suggestions on how we should do this?

Thanks

Neil



--
You received this message because you are subscribed to the Google Groups "Castle 
Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to