views:

42

answers:

1

Hello,

I'm running a few unit tests that requires a connection to the database. When my test project get initialized, a snapshot of the database is created, and when tests are done the database gets restored back to the snapshot.

Here is the implementation:

[TestClass]
public static class AssemblyInitializer
{
    [AssemblyInitialize()]
    public static void AssemblyInit(TestContext context)
    {
        var dbss = new DatabaseSnapshot(...);    
        dbss.CreateSnapshot();
    }

    [AssemblyCleanup()]
    public static void AssemblyCleanup()
    {
        var dbss = new DatabaseSnapshot(...);
        dbss.RevertDatabase();
    }
}

Now this all works, but my problem arise when I have a failing test or some exception. The AssemblyCleanup is of course not invoked, so how can I solve this problem? No matter what happens, the snapshot has to be restored. Is this possible?

A: 

Yes, don't do it this way. Somebody might trip over the power cord. Always copy a known-good copy of the database files and attach them. Look in the documentation for the dbase engine you use how to attach.

Hans Passant
You are right. Maybe I should invest more time in mocking the ObjectContext.
Tommy Jakobsen