Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9031535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:45:21+00:00 2026-06-16T07:45:21+00:00

We have an MVC 3, .Net 4.0 application and a number of specflow tests

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:45:22+00:00Added an answer on June 16, 2026 at 7:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ASP.NET MVC application I have a number of threads that wait for
in a ASP.NET application (MVC) I have a foreach loop that loops through a
I have an ASP.NET MVC application that uses Fluent NHibernate and AutoMapper. I am
I have an Asp.Net MVC application that works in the vs.net development web server.
I have a ASP.NET MVC application that runs in both IIS 6 and 7.
I have a basic ASP.Net MVC 3 application which has a number of controllers
I have an existing ASP.NET web application that I'm converting to MVC 1.0. The
I just got started running UI tests against my ASP.NET MVC application using WatiN.
I am writing an ASP.NET MVC 3 application and I have a number of
I have an ASP.NET MVC application that utilizes NHiberante and SQL Server 2008 on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.