Skip to content

Instantly share code, notes, and snippets.

@aidanmorgan
Created October 20, 2012 10:08

Revisions

  1. aidanmorgan created this gist Oct 20, 2012.
    92 changes: 92 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    public class IntegrationTestUnitOfWorkFactory : IUnitOfWorkFactory
    {
    /// <summary>
    /// This <see cref="Guid"/> is used to uniquely identify this factory instance when interacting with the LocalDB instance.
    /// </summary>
    private readonly Guid _instanceGuid = Guid.NewGuid();

    /// <summary>
    /// The <see cref="TestContext"/> instance that describes the environment in which the tests are currently running.
    /// This is used to get the deployment directory for where the LocalDB file should be stored.
    /// </summary>
    private readonly TestContext _testContext;

    public IntegrationTestUnitOfWorkFactory(TestContext testContextInstance)
    {
    _testContext = testContextInstance;
    }

    /// <summary>
    /// Returns the name of the database inside LocalDB.
    /// </summary>
    private string DatabaseName
    {
    get { return _testContext.TestName + _instanceGuid; }
    }

    /// <summary>
    /// Returns the filename to use for the LocalDB file.
    /// </summary>
    private string DatabaseFile
    {
    get { return DatabaseName + ".sdf"; }
    }

    /// <summary>
    /// Returns the directory in which the LocalDB database should be stored.
    /// </summary>
    private string DatabaseDirectory
    {
    get { return _testContext.TestDeploymentDir; }
    }

    /// <summary>
    /// Returns the fill path to the LocalDB file on the filesystem.
    /// </summary>
    private string FullDatabasePath
    {
    get
    {
    return string.Format(@"{0}/{1}", DatabaseDirectory, DatabaseFile);
    }
    }

    /// <summary>
    /// Returns the connection string to use to connect to the database.
    /// </summary>
    private string ConnectionString
    {
    get
    {
    return
    string.Format(
    @"Data Source=(LocalDB)\v11.0;Initial Catalog={0};AttachDbFilename=""{1}"";Integrated Security=True",
    DatabaseName, FullDatabasePath);
    }
    }

    /// <summary>
    /// Creates a new <see cref="IUnitOfWork"/> instance, bound to the <see cref="ProjectDbContext"/>.
    /// </summary>
    /// <returns>a new <see cref="IUnitOfWork"/> instance, bound to the <see cref="ProjectDbContext"/>.</returns>
    public IUnitOfWork Create()
    {
    ProjectDbContext context = new ProjectDbContext(ConnectionString);

    if (!context.Database.Exists())
    {
    context.Database.Create();
    }

    return new Ef5UnitOfWork(context);
    }

    /// <summary>
    /// Deletes the database that was used for this <see cref="IntegrationTestUnitOfWorkFactory"/>.
    /// </summary>
    public void Cleanup()
    {
    ProjectDbContext context = new ProjectDbContext(ConnectionString);
    context.Database.Delete();
    }
    }