Created
July 28, 2015 03:52
-
-
Save sdanna/3b8062146443443dff8f to your computer and use it in GitHub Desktop.
Generic XUnit ClassFixture
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 DatabaseFixture<T> : IDisposable | |
{ | |
public DatabaseFixture<T>() | |
{ | |
// Do something interesting with the Generic parameter like initialize | |
// a database dynamically based on generic type. | |
Db = new SqlConnection("MyConnectionString"); | |
// ... initialize data in the test database ... | |
} | |
public void Dispose() | |
{ | |
// ... clean up test data from the database ... | |
} | |
public SqlConnection Db { get; private set; } | |
} | |
public class MyDatabaseTests : IClassFixture<DatabaseFixture<MyDatabaseTests>>, IDisposable | |
{ | |
DatabaseFixture<MyDatabaseTests> fixture; | |
public MyDatabaseTests(DatabaseFixture<MyDatabaseTests> fixture) | |
{ | |
this.fixture = fixture; | |
} | |
// ... write tests, using fixture.Db to get access to the SQL Server ... | |
public void Dispose() | |
{ | |
fixture.Respawn(); // Respawn logic to clear out your data. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment