3

We have an MVC 3, .Net 4.0 application and a number of specflow tests in xunit that seem to be running with no problem. To be in full control of our test data, we want to setup a clean database at the beginning of each scenario and dispose of it afterwards. For that, we need to change the connection string on the fly before each scenario. Connections to the database are handled using NHibernate sessions and we used the following code to change the connection string:

public class SessionFactoryProvider
{
    private static ISessionFactory _sessionFactory;
    public static ISessionFactory BuildSessionFactory(bool resetConnection = false)
    {
        if (ConnectionString == null)
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }

        if (_sessionFactory == null || resetConnection)
        {
            _sessionFactory = Fluently.Configure()
                               .Mappings(x => x.FluentMappings.AddFromAssemblyOf<InvoiceMap>().Conventions.AddFromAssemblyOf<CascadeConvention>())
                               .Database(MsSqlConfiguration.MsSql2008.ConnectionString(ConnectionString))
                               .ExposeConfiguration(UpdateSchema)
                               .CurrentSessionContext("web")
                               .BuildSessionFactory();
        }
        return _sessionFactory;
    }
}
[BeforeScenario]
public static void Setup_Database()
    {
        var connection = DBHandler.GetAcceptanceDatabaseConnection();
        SessionFactoryProvider.ConnectionString = connection.ConnectionString;
        var session = SessionFactoryProvider.BuildSessionFactory(true).OpenSession();

    }

But it looks like the Specflow tests and the actual application are running as two different processes and they don't share the same _sessionFactory although it's defined as static. So changing the connection string in the Setup_Database function changes the session of the specflow tests' process not the connection string that the application process is using.

  1. Are there any better methods for acceptance test data population?
  2. Does our approach for switching connection strings make sense?
  3. Is it possible for the Specflow tests to manipulate the session of the application itself?
Shermin
  • 158
  • 7

1 Answers1

0

It does seem a little strange to have to fiddle with connection strings while your tests are running.

An alternative pattern you may consider is to use the same database for all your tests, but to use TransactionScope to clean up after every test. You do this by opening a new transaction for each test and then disposing of the transaction after the test has run. (You can move this into a base class to avoid duplicating the logic.) This will ensure each test has a clean DB.

I'm afraid I haven't used this with NHibernate, but you can look at my answer to this question to see an example with EntityFramework and MSTest. This short blog post by Ayende Rahien may be helpful to understand how NHibernate and TransactionScope can work together.

Community
  • 1
  • 1
rouan
  • 5,339
  • 6
  • 22
  • 36