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.
- Are there any better methods for acceptance test data population?
- Does our approach for switching connection strings make sense?
- Is it possible for the Specflow tests to manipulate the session of the application itself?