Created
October 20, 2012 10:08
-
-
Save aidanmorgan/3922845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment